创建了一个用户验证器 <?php namespace app\common\validate; use think\Validate; class User extends Validate { protected $rule = [ 'email|邮箱'=>'email|unique:email', 'pass|密码'=>'require|min:6', 'phone|手机号'=>'regex:^1\d{10}|unique:phone', ]; protected $message = [ 'email.email'=>'邮箱格式有误', 'email.unique'=>'邮箱已存在', 'pass'=>'密码只是6位', 'phone.regex'=>'手机号码格式有误', 'phone.unique'=>'手机号码已存在', ]; protected $scene = [ //登录 'signin'=>['email'=>'email','pass'=>'require|min:6'], ]; }
然后在控制器中调用User验证器中的signin进行验证
<?php namespace app\index\controller; use think\Controller; use think\Request; use app\common\validate\User as UserValidate; use think\Log; use app\common\model\User as UserModel; class Login extends Controller { public function login() { if(Request::instance()->isAjax()){ $data = [ 'email'=>Request::instance()->param('account'), 'pass'=>Request::instance()->param('password') ]; $validate = new UserValidate(); Log::log($validate->scene('User.signin')->check($data)); if(!$validate->scene('User.signin')->check($data)){ dump($validate->getError()); } } }
然后前端通过jquery 的$.post调用 报错了 为啥把要验证的email字段变成了表名fl_email这表是不存在的因为要验证的是user表的email字段
[42S02]SQLSTATE[42S02]: Base table or view not found: 1146 Table 'fldatabase.fl_email' doesn't exist[E:\phpStudy\WWW\thinkphp\library\think\db\connector\Mysql.php:65]