thinkphp验证器

Posted 虚无缥缈的云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thinkphp验证器相关的知识,希望对你有一定的参考价值。

验证器类:$validate=new hinkValidate($rule,$message,$field);

独立验证:

//独立验证
$rule=[
    ‘name‘  => ‘require|max:25‘,
    ‘email‘ => ‘email‘
]
$msg=[
    ‘name.require‘ => ‘名称必须‘,
    ‘name.max‘     => ‘名称最多不能超过25个字符‘,
    ‘email‘        => ‘邮箱格式错误‘,    
];
$field = [
    ‘name‘  => ‘名称‘,
    ‘email‘ => ‘邮箱‘,    
];
$validate = new Validate($rule,$msg,$field); 
$data = [
    ‘name‘ => ‘thinkphp‘,
    ‘email‘ => ‘[email protected]
]; 
if (!$validate->check($data)) {
    dump($validate->getError());
}

 

make、rule、extend、setTypeMsg、message、scene、hasscene、batcch、check、getError

验证器:定义在模块下的validate文件夹内;

注意:Call to undefined function mb_strlen() :开启:extension=php_mbstring.dll; 还不行就就看看这extension_dir = "ext";还不行就绝对路径

 

前台页面

<form action="{:url(‘addFormData‘)}" method="post">
    <input type="text" name="name" value="thinkphp">
    <input type="text" name="email" value="[email protected]">
    <input type="text" name="zip" value="888888">
    <input type="text" name="age" value="20">
    <input type="text" name="status" value="2">
    {:token(‘__token__‘, ‘sha1‘)}
    <input type="submit" value="提交">
</form>

控制器:

<?php
namespace appindexcontroller;
use thinkController;
class Test extends Controller{

    public function index()
    {
        
        return $this->fetch(‘[email protected]/Test/index‘);
    }


    public function addFormData(	hinkRequest $request)
    {

        $name=$request->post(‘name‘);
        $email=$request->post(‘email‘);
        $zip=$request->post(‘zip‘);
        $age=$request->post(‘age‘);
        $status=$request->post(‘status‘);
        $token=$request->post(‘__token__‘);
        

        $data = [
            ‘name‘=>$name,
            ‘email‘=>$email,
            ‘zip‘=>$zip,
            ‘age‘=>$age,
            ‘status‘=>$status,
            ‘__token__‘=>$token
        ];

        $validate = 	hinkLoader::validate(‘test‘);//or  $validate = validate(‘test‘);
        //实例化后还可以动态添加规则 他将合并到Test验证rule属性里
        $validate->rule([
            ‘zip‘   => ‘/^d{6}$/‘,
            ‘status‘=> ‘number|between:0,2‘
        ]);
        //实例化后还可以动态添加错误信息 他将合并到Test验证$message属性里
        $validate->message([
            ‘zip‘=>‘邮编格式不正确‘,
            ‘status.between‘=>‘状态超出取值范围‘,
            ‘status.number‘=>‘状态数据类型错误‘,
        ]);
     //实例化后可以动态添加自定义的验证字段描述
        $validate->addField([
            ‘zip‘=>‘邮编‘,
            ‘status‘=>‘状态‘,
        ]);

        if(!$validate->check($data)){
            dump($validate->getError());
        }        
    }
}

验证器:

<?php
namespace appindexvalidate;

use thinkValidate;

class Test extends Validate
{
    //定义验证规则   |和数组都可以
    protected $rule = [
        ‘name‘  =>  ‘require|max:25‘,
        ‘email‘ =>  ‘email‘,
        ‘age‘   => [‘number‘,‘between‘=>‘1,120‘],
        ‘__token__‘=>‘token‘
    ];

    //定义错误信息
    protected $message  =   [
        ‘name.require‘ => ‘名称必须‘,
        ‘name.max‘     => ‘名称最多不能超过25个字符‘,
        ‘age.number‘   => ‘年龄必须是数字‘,
        ‘age.between‘  => ‘年龄只能在1-120之间‘,
        ‘email‘        => ‘邮箱格式错误‘,    
    ];
    //定义验证字段描述
    protected $field=[
         ‘name‘  => ‘名称‘,
        ‘age‘   => ‘年龄‘,
        ‘email‘ => ‘邮箱‘,    
    ];

    //定义场景(后面根数组是重新定义规则)
    protected $scene = [
        ‘edit‘  =>  [‘name‘,‘email‘,‘age‘=>‘require|number|between:1,120‘],
    ];


    /**
     * 自定义动态添加验证字段描述
     */
    public function addField($name,$field=‘‘)        
    {
        if (is_array($name)) {
            $this->field = array_merge($this->field, $name);
        } else {
            $this->field[$name] = $field;
        }
        return $this;
    }

}

 

以上是关于thinkphp验证器的主要内容,如果未能解决你的问题,请参考以下文章

thinkphp5.0--验证

thinkphp验证功能(部分)

如何解决thinkphp5中验证码常见问题

auth 权限验证求助

thinkphp3.2验证码怎么写

ThinkPHP框架之验证码