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.

95 lines
2.3 KiB

7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class AdminRoleRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'name' => 'required|max:30',
  9. 'des' => 'max:150',
  10. 'status' => 'integer|between:0,2',
  11. 'pid' => 'integer',
  12. 'listorder' => 'integer|between:0,9999',
  13. ];
  14. //总的自定义错误信息
  15. protected $messages = [
  16. 'id.required' => 'ID必填',
  17. 'id.integer' => 'ID必须为数字',
  18. 'name.required' => '角色名必填',
  19. 'name.max' => '角色名不能大于30个字',
  20. 'des.max' => '描述不能大于150个字',
  21. 'status.integer' => '状态必须是数字',
  22. 'status.between' => '状态,0启用,1禁用',
  23. 'pid.integer' => '父级ID必须为数字',
  24. 'listorder.integer' => '排序必须是数字',
  25. 'listorder.between' => '排序只能0-9999',
  26. ];
  27. //场景验证规则
  28. protected $scene = [
  29. 'add' => ['name', 'des', 'status', 'pid', 'listorder'],
  30. 'edit' => ['name', 'des', 'status', 'pid', 'listorder'],
  31. 'del' => ['id'],
  32. ];
  33. /**
  34. * Determine if the user is authorized to make this request.
  35. *
  36. * @return bool
  37. */
  38. public function authorize()
  39. {
  40. return true; //修改为true
  41. }
  42. /**
  43. * Get the validation rules that apply to the request.
  44. *
  45. * @return array
  46. */
  47. public function rules()
  48. {
  49. return $this->rules;
  50. }
  51. /**
  52. * 获取被定义验证规则的错误消息.
  53. *
  54. * @return array
  55. */
  56. public function messages()
  57. {
  58. return $this->messages;
  59. }
  60. //获取场景验证规则
  61. public function getSceneRules($name, $fields = null)
  62. {
  63. $res = array();
  64. if(!isset($this->scene[$name]))
  65. {
  66. return false;
  67. }
  68. $scene = $this->scene[$name];
  69. if($fields != null && is_array($fields))
  70. {
  71. $scene = $fields;
  72. }
  73. foreach($scene as $k=>$v)
  74. {
  75. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  76. }
  77. return $res;
  78. }
  79. //获取场景验证规则自定义错误信息
  80. public function getSceneRulesMessages()
  81. {
  82. return $this->messages;
  83. }
  84. }