封装方法:
//参数验证
function my_validate($data,$rule,$msg = []){
$validate = \think\facade\Validate::rule($rule)->message($msg);
$bool = $validate->check($data);
if (!$bool){
exit(json_encode(['code'=>MSG_PARAMS_ERROR,'msg'=>$validate->getError()],JSON_UNESCAPED_UNICODE));
}
}
进行调用:
//tp验证类使用
public function test2(){
$params = ['name'=>'','info'=>['id'=>[1]]];
$rule = [
'name'=>'require|number',
'info.id'=>'require|array'
];
$message = [
'name.require'=>'错误信息',
];
my_validate($params,$rule,$message);
// $validate = Validate::rule($rule)->message($message);
// $bool = $validate->check($params);
// if (!$bool){
// dump($validate->getError());
//
// }
}