check() 공개 메소드

数据自动验证
public check ( array $data, mixed $rules = [], string $scene = '' ) : boolean
$data array 数据
$rules mixed 验证规则
$scene string 验证场景
리턴 boolean
예제 #1
0
 public function testScene()
 {
     $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' => '*****@*****.**'];
     $validate = new Validate($rule);
     $validate->scene('edit', ['name', 'age']);
     $validate->scene('edit');
     $result = $validate->check($data);
     $this->assertEquals(true, $result);
 }
예제 #2
0
 public function testCheck()
 {
     $data = ['username' => 'username', 'nickname' => 'nickname', 'password' => '123456', 'repassword' => '123456', 'mobile' => '13800000000', 'email' => '*****@*****.**', 'sex' => '0', 'age' => '20', 'code' => '1234', 'test' => ['a' => 1, 'b' => 2]];
     $validate = ['__pattern__' => ['mobile' => '/^1(?:[358]\\d|7[6-8])\\d{8}$/', 'require' => '/.+/'], '__all__' => ['code' => function ($value, $data) {
         return '1234' != $value ? 'code error' : true;
     }], 'user' => [['username', [&$this, 'checkName'], '用户名长度为5到15个字符', 'callback', 'username'], ['nickname', 'require', '请填昵称'], 'password' => ['[\\w-]{6,15}', '密码长度为6到15个字符'], 'repassword' => ['password', '两次密码不一到致', 'confirm'], 'mobile' => ['mobile', '手机号错误'], 'email' => ['validate_email', '邮箱格式错误', 'filter'], 'sex' => ['0,1', '性别只能为为男或女', 'in'], 'age' => ['1,80', '年龄只能在10-80之间', 'between'], 'test.a' => ['number', 'a必须是数字'], 'test.b' => ['1,3', '不能是1或者3', 'notin'], '__option__' => ['scene' => ['add' => 'username,nickname,password,repassword,mobile,email,age,code', 'edit' => 'nickname,password,repassword,mobile,email,sex,age,code'], 'value_validate' => 'email', 'exists_validate' => 'password,repassword,code']]];
     Config::set('validate', $validate);
     Validate::check($data, 'user.add');
     $this->assertEquals([], Validate::getError());
     unset($data['password'], $data['repassword']);
     $data['email'] = '';
     Validate::check($data, 'user.edit');
     $this->assertEquals([], Validate::getError());
 }