MemberValidator.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Http\Validator;
  3. class MemberValidator extends BaseValidator
  4. {
  5. // 当前模型所有验证规则
  6. public static $rules = [
  7. 'id' => 'required',
  8. 'username' => 'required|username|min:2|max:20',
  9. 'password' => 'required|max:30',
  10. 'code' => 'required',
  11. 'mobile'=> 'required|mobile',
  12. 'email'=> 'required|email',
  13. ];
  14. // 当前模型所有错误提示信息
  15. public static $msgs = [
  16. 'required' => ':attribute不能为空',
  17. 'string' => ':attribute必须是字符串',
  18. 'min' => ':attribute长度不能小于:min位',
  19. 'max' => ':attribute长度不能大于:max位',
  20. 'exists' => ':attribute不存在',
  21. 'rule' => ':attribute格式不正确',
  22. 'mobile' => ':attribute格式不正确',
  23. 'email' => ':attribute格式不正确',
  24. 'username' => ':attribute格式不正确',
  25. ];
  26. // 当前模型所有验证字段
  27. public static $fields = [
  28. 'id' => 'ID',
  29. 'password' => '登录密码',
  30. 'username' => '用户名',
  31. 'code' => '验证码',
  32. 'mobile' => '手机号码',
  33. 'email' => '邮箱',
  34. ];
  35. // 当前模型所有验证场景
  36. public static $scenes = [
  37. 'info'=> ['id'],
  38. 'register'=> ['username','password','code'],
  39. 'login'=> ['username','password'],
  40. 'forget'=> ['username','code'],
  41. 'save'=> ['username','realname','gender'],
  42. 'mobile'=> ['mobile'],
  43. 'email'=> ['email'],
  44. ];
  45. /**
  46. * 验证
  47. * @param $request
  48. * @param string $scene
  49. * @return int|mixed
  50. */
  51. public static function check($request, $scene=''){
  52. $validator = new BaseValidator(self::$rules, self::$msgs, self::$fields, self::$scenes);
  53. return $validator->checkParams($request, $scene);
  54. }
  55. }