TP5验证可分为独立验证和验证器;
独立验证是可直接写在控制器里直接验证如下:
//独立验证
$data = [ 'name'=>'vendor33333', 'email'=>'vaendor@qq.com' ]; $validate = new Validate([ 'name'=>'require|max:10', 'email'=>'email' ]); $result = $validate->batch()->check($dataprint_r($validate->getError()); }
//验证器验证特点:对规则作了封装
模块目录下建立validate文件夹,再新建如TestValidate类,内容如下:
namespace appapivalidate; use thinkValidate; class TestValidate extends Validate { protected $rule = [ 'name' =>'require|max:10', 'email'=>'email' ]; }
//以下为控制器中的内容
$validate = new TestValidate();
$result = $validate->batch()->check($data);
print_r($validate->getError());