tp6 单独验证
$rule =[
'字段'=>'require'
]
try{
$v = new Validate();
$v->rule($rule)->failException(true)->check($param);
}catch (ValidateException $e) {
// 验证失败 输出错误信息
return ['code'=>103,'msg'=>$e->getError()];
}
tp6 方法验证器
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
[$validate, $scene] = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}