一:利用validate函数例如 $validate = validate('验证器名称,首字母大写');还可以有验证场景
if(!validate->check('post.')){
$this->error($validate->getError());
}else{
}
二
$data = $this->request->only(['username', 'password', 'verify']);
$validate_result = $this->validate($data, 'Login');
if ($validate_result !== true) {
$this->error($validate_result);
}
验证器的写法
1.
protected $rule = [
'username' => 'require',
'password' => 'require',
'verify' => 'require|captcha'
];
protected $message = [
'username.require' => '请输入用户名',
'password.require' => '请输入密码',
'verify.require' => '请输入验证码',
'verify.captcha' => '验证码不正确'
];
2.连写
protected $rule = [
['username', 'require|max:1000', '用户名必须传递|分类名不能超过10个字符'],
['password','require|confirm','密码必须传递|两次密码不一致'],
['mobile','require|number','手机号必填写|手机号必须是数字'],
['email', 'require|email','邮箱必填写|邮箱格式错误'],
];
/**场景设置**/
protected $scene = [
'add' => ['name', 'parent_id', 'id'],// 添加
'listorder' => ['id', 'listorder'], //排序
'status' => ['id', 'status'],
];