1、表單提交:
<input name="username" type="text" id="user" placeholder="用戶名"> $username = $this->request->post('username');
post接收的值,就是表單input裏面的name值。php
超級簡單的接收表單數據方法:thinkphp
use think\facade\Request; public function check() { $req = Request::param(); }
2、驗證,thinkphp裏面提供驗證器,手冊位置:驗證——驗證器數組
首先創建驗證器及其驗證規則app
<?php namespace app\common\validate; use \think\Validate; class AdminLogin extends Validate { protected $rule = [ 'username' => 'require', 'password' => 'require', ]; protected $message = [ 'username.require' => '用戶名不能爲空', 'password.require' => '密碼不能爲空', ]; }
注意:繼承的的是\think\Validate類。post
$rule爲驗證規則,
$message爲提示信息。
public function check() { //接收表單數據 //$username = $this->request->post('username'); //$password = $this->request->post('password'); //把接收數據存入數組以便驗證 $r['username'] = $this->request->post('username'); $r['password'] = $this->request->post('password'); //驗證填寫信息,首先實例化驗證器對象,而後判斷時候複合驗證器裏面的規則 $validate = new AdminLogin; if (!$validate->check($r)) { return $this->error($validate->getError()); } return '132'; }
實例化對象後,經過check($data)方法驗證數據信息是否符合要求。其中$data是表單接收過來的信息ui