You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class AdminRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'name' => 'required|max:30',
  9. 'email' => 'required|max:30',
  10. 'login_time' => 'integer',
  11. 'pwd' => 'required|max:32',
  12. 'role_id' => 'required|integer',
  13. 'status' => 'integer|between:0,3',
  14. 'mobile' => 'max:20',
  15. 'avatar' => 'max:150',
  16. 'add_time' => 'required|integer',
  17. ];
  18. //总的自定义错误信息
  19. protected $messages = [
  20. 'id.required' => 'ID必填',
  21. 'id.integer' => 'ID必须为数字',
  22. 'name.required' => '用户名必填',
  23. 'name.max' => '用户名不能超过30个字符',
  24. 'email.required' => '邮箱必填',
  25. 'email.max' => '邮箱不能超过30个字符',
  26. 'login_time.integer' => '登录时间必须是数字',
  27. 'pwd.required' => '密码必填',
  28. 'pwd.max' => '密码不能超过32个字符',
  29. 'role_id.required' => '角色ID必填',
  30. 'role_id.integer' => '角色ID必须为数字',
  31. 'status.integer' => '用户状态必须是数字',
  32. 'status.between' => '用户状态 0:正常; 1:禁用 ;2:未验证',
  33. 'mobile.max' => '手机号不能超过20个字符',
  34. 'avatar.max' => '头像不能超过150个字符',
  35. 'add_time.required' => '添加时间必填',
  36. 'add_time.integer' => '添加时间必须是数字',
  37. ];
  38. //场景验证规则
  39. protected $scene = [
  40. 'add' => ['name', 'email', 'login_time', 'pwd', 'role_id', 'status', 'mobile', 'avatar'],
  41. 'edit' => ['name', 'email', 'login_time', 'pwd', 'role_id', 'status', 'mobile', 'avatar'],
  42. 'del' => ['id'],
  43. ];
  44. /**
  45. * Determine if the user is authorized to make this request.
  46. *
  47. * @return bool
  48. */
  49. public function authorize()
  50. {
  51. return true; //修改为true
  52. }
  53. /**
  54. * Get the validation rules that apply to the request.
  55. *
  56. * @return array
  57. */
  58. public function rules()
  59. {
  60. return $this->rules;
  61. }
  62. /**
  63. * 获取被定义验证规则的错误消息.
  64. *
  65. * @return array
  66. */
  67. public function messages()
  68. {
  69. return $this->messages;
  70. }
  71. //获取场景验证规则
  72. public function getSceneRules($name, $fields = null)
  73. {
  74. $res = array();
  75. if(!isset($this->scene[$name]))
  76. {
  77. return false;
  78. }
  79. $scene = $this->scene[$name];
  80. if($fields != null && is_array($fields))
  81. {
  82. $scene = $fields;
  83. }
  84. foreach($scene as $k=>$v)
  85. {
  86. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  87. }
  88. return $res;
  89. }
  90. //获取场景验证规则自定义错误信息
  91. public function getSceneRulesMessages()
  92. {
  93. return $this->messages;
  94. }
  95. }