Thinkphp5.1 表单提交、验证

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

相关文章
相关标签/搜索