tp5—验证器与验证规则

概述

ThinkPHP5.0验证使用独立的\think\Validate类或者验证器进行验证。

独立验证任何时候,都可以使用Validate类进行独立的验证操作,例如:

$validate = new Validate([
‘name’ => ‘require|max:25’,
‘email’ => ‘email’
]);
d a t a = [ ′ n a m e ′ = > ′ t h i n k p h p ′ , ′ e m a i l ′ = > ′ t h i n k p h p @ q q . c o m ′ ] ; i f ( ! data = [ 'name' => 'thinkphp', 'email' => '[email protected]' ]; if (! data=[name=>thinkphp,email=>thinkphp@qq.com];if(!validate->check(KaTeX parse error: Expected '}', got 'EOF' at end of input: …a)) { dump(validate->getError());
}

验证器

这是5.0推荐的验证方式,为具体的验证场景或者数据表定义好验证器类,直接调用验证类的check方法即可完成验证,下面是一个例子:
我们定义一个\app\index\validate\User验证器类用于User的验证。
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
protected $rule = [
‘name’ => ‘require|max:25’,
‘email’ => ‘email’,
];

}
在需要进行User验证的地方,添加如下代码即可:
$data = [
‘name’=>‘thinkphp’,
‘email’=>‘[email protected]
];

$validate = Loader::validate(‘User’);

if(! v a l i d a t e − > c h e c k ( validate->check( validate>check(data)){
dump($validate->getError());
}
使用助手函数实例化验证器
$validate = validate(‘User’);

验证规则:

设置规则

可以在实例化Validate类的时候传入验证规则,例如:
$rules = [
‘name’ => ‘require|max:25’,
‘age’ => ‘number|between:1,120’,
];
v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rules);
也可以使用rule方法动态添加规则,例如:
$rules = [
‘name’ => ‘require|max:25’,
‘age’ => ‘number|between:1,120’,
];
v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rules);
KaTeX parse error: Expected group after '^' at position 25: …>rule('zip', '/^̲\d{6}/’);
$validate->rule([
‘email’ => ‘email’,
]);

规则定义

规则定义支持下面两种方式:
$rules = [
‘name’ => ‘require|max:25’,
‘age’ => ‘number|between:1,120’,
];
v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rules);
对于一个字段可以设置多个验证规则,使用|分割。
或者采用数组方式定义多个规则(适用于你的验证规则中有|的情况)
$rules = [
‘name’ => [‘require’,‘max’=>25],
‘age’ => [‘number’,‘between’=>‘1,120’],
];
v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rules);

属性定义

通常情况下,我们实际在定义验证类的时候,可以通过属性的方式直接定义验证规则等信息,例如:
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
protected $rule = [
‘name’ => ‘require|max:25’,
‘age’ => ‘number|between:1,120’,
‘email’ => ‘email’,
];

protected $message  =   [
    'name.require' => '名称必须',
    'name.max'     => '名称最多不能超过25个字符',
    'age.number'   => '年龄必须是数字',
    'age.between'  => '年龄只能在1-120之间',
    'email'        => '邮箱格式错误',    
];

}

验证数据

下面是一个典型的验证数据的例子:
$rule = [
‘name’ => ‘require|max:25’,
‘age’ => ‘number|between:1,120’,
‘email’ => ‘email’,
];

$msg = [
‘name.require’ => ‘名称必须’,
‘name.max’ => ‘名称最多不能超过25个字符’,
‘age.number’ => ‘年龄必须是数字’,
‘age.between’ => ‘年龄只能在1-120之间’,
‘email’ => ‘邮箱格式错误’,
];

$data = [
‘name’ => ‘thinkphp’,
‘age’ => 10,
‘email’ => ‘[email protected]’,
];

v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rule, $msg);
$result = v a l i d a t e − > c h e c k ( validate->check( validate>check(data);
如果需要批量验证,可以使用:
v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rule, $msg);
$result = v a l i d a t e − > b a t c h ( ) − > c h e c k ( validate->batch()->check( validate>batch()>check(data);
批量验证如果验证不通过,返回的是一个错误信息的数组。
如果你定义了User验证器类的话,可以使用下面的验证代码:
$data = [
‘name’ => ‘thinkphp’,
‘age’ => 10,
‘email’ => ‘[email protected]’,
];
v a l i d a t e = L o a d e r : : v a l i d a t e ( ′ U s e r ′ ) ; i f ( ! validate = Loader::validate('User'); if(! validate=Loader::validate(User);if(!validate->check(KaTeX parse error: Expected '}', got 'EOF' at end of input: …ta)){ dump(validate->getError());
}

闭包函数验证

可以对某个字段使用闭包验证,例如:
KaTeX parse error: Expected 'EOF', got '\think' at position 16: validate = new \̲t̲h̲i̲n̲k̲\Validate([ …value,KaTeX parse error: Expected '}', got 'EOF' at end of input: …rn 'thinkphp'==value ? true : false;
},
]);
第一个参数是当前字段的值,第二个参数是全部数据

自定义验证规则

系统内置了一些常用的规则,如果还不够用,可以自己扩展验证规则。
如果使用了验证器的话,可以直接在验证器类添加自己的验证方法,例如:
namespace app\index\validate;

use think\Validate;

class User extends Validate
{
protected $rule = [
‘name’ => ‘checkName:thinkphp’,
‘email’ => ‘email’,
];

protected $message = [
    'name'  =>  '用户名必须',
    'email' =>  '邮箱格式错误',
];

// 自定义验证规则
protected function checkName($value,$rule,$data)
{
    return $rule == $value ? true : '名称错误';
}

}
验证方法可以传入的参数共有5个(后面三个根据情况选用),依次为:
验证数据
验证规则
全部数据(数组)
字段名
字段描述
并且需要注意的是,自定义的验证规则方法名不能和已有的规则冲突。
接下来,就可以这样进行验证:
v a l i d a t e = L o a d e r : : v a l i d a t e ( ′ U s e r ′ ) ; i f ( ! validate = Loader::validate('User'); if(! validate=Loader::validate(User);if(!validate->check(KaTeX parse error: Expected '}', got 'EOF' at end of input: …ta)){ dump(validate->getError());
}
如果没有使用验证器类,则支持使用extend方法扩展验证规则,例如:
$validate = new Validate([‘name’ => ‘checkName:1’]);
v a l i d a t e − > e x t e n d ( ′ c h e c k N a m e ′ , f u n c t i o n ( validate->extend('checkName', function ( validate>extend(checkName,function(value, $rule) {
return $rule == $value ? true : ‘名称错误’;
});
$data = [‘name’ => 1];
$result = v a l i d a t e − > c h e c k ( validate->check( validate>check(data);
支持批量注册验证规则,例如:
$validate = new Validate([‘name’ => ‘checkName:1’]);
v a l i d a t e − > e x t e n d ( [ ′ c h e c k N a m e ′ = > f u n c t i o n ( validate->extend([ 'checkName'=> function ( validate>extend([checkName=>function(value, $rule) {
return $rule == KaTeX parse error: Expected 'EOF', got '}' at position 24: …true : '名称错误'; }̲, 'checkSta…this,‘checkStatus’]
]);
$data = [‘name’ => 1];
$result = v a l i d a t e − > c h e c k ( validate->check( validate>check(data);

设置字段信息

V5.0.4+版本开始,验证类的架构方法支持传入field参数批量设置字段的描述信息,例如:
$rule = [
‘name’ => ‘require|max:25’,
‘age’ => ‘number|between:1,120’,
‘email’ => ‘email’,
];

$field = [
‘name’ => ‘名称’,
‘age’ => ‘年龄’,
‘email’ => ‘邮箱’,
];

$data = [
‘name’ => ‘thinkphp’,
‘age’ => 10,
‘email’ => ‘[email protected]’,
];

v a l i d a t e = n e w V a l i d a t e ( validate = new Validate( validate=newValidate(rule, [] , $field);
$result = v a l i d a t e − > c h e c k ( validate->check( validate>check(data);

你可能感兴趣的:(tp5—验证器与验证规则)