一、分类:
1、独立验证
2、验证器
二、代码描述
1.独立验证
$data1 = [
'name'=>'chenzhitao',
'email'=>'zhitaochan@163.com'
];
$validate = new Validate([
'name'=>'require|max:10',
'email'=>'email',
]);
//批量验证
$result = $validate->batch()->check($data1);
// var_dump( $validate->getError());
2.验证器
$data2 = [
'name'=>'chenzhitaoww',
'email'=>'zhitaochan163.com'
];
$validate2 = new TestValidate($data2);
$result = $validate2->batch()->check($data1);
var_dump( $validate2->getError());
//附验证器TestValidate类
<?php
/**
* Created by PhpStorm.
* User: chenzhitao
* Date: 2017/5/7
* Time: 下午4:33
*/
namespace app\api\validate;
use think\Validate;
class TestValidate extends Validate
{
protected $rules = [
'name'=>'require|max:10',
'email'=>'email',
];
}
三、比较
验证器比独立验证封装性更好
网友评论