tp6控制器里直接调用验证器
public function vilidate_test(){ $phone = input('phone'); $pwd = input('pwd'); $pwd_confirm = input('pwd_confirm'); $data = [ 'phone' => $phone ,'pwd' => $pwd ,'pwd_confirm' => $pwd_confirm ]; $rule = [ 'phone' => 'require|mobile' ,'pwd' => ['require','confirm','regex'=>'/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-z]|[A-Z]|[0-9]){6,}$/'] // tp6验证器规定‘如果正则表达式中包含有|符号的话,必须使用数组方式定义’ ]; $fun_type = 2;// 推荐用方法2省得写异常捕获 if($fun_type == 1){ // 方法1:基础BaseController控制器里的validate函数需要用try捕获不然验证失败会直接抛出异常(需要继承BaseController才可以用) try { $result = $this->validate($data,$rule); } catch (\Exception $e){ print_r($e->getMessage());// 验证失败原因 } } else if($fun_type == 2){ // 方法二:tp里的Validate类 $validate = \think\facade\Validate::rule($rule); if (!$validate->check($data)) { print_r($validate->getError());// 验证失败原因 } } }