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.

101 lines
2.6 KiB

6 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class UserMessageRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'user_id' => 'required|integer',
  9. 'type' => 'integer|between:0,9',
  10. 'status' => 'integer|between:0,9',
  11. 'title' => 'max:150',
  12. 'des' => 'required|max:250',
  13. 'litpic' => 'max:100',
  14. 'add_time' => 'required|integer',
  15. ];
  16. //总的自定义错误信息
  17. protected $messages = [
  18. 'id.required' => 'ID必填',
  19. 'id.integer' => 'ID必须为数字',
  20. 'user_id.required' => '用户ID必填',
  21. 'user_id.integer' => '用户ID必须为数字',
  22. 'type.integer' => '消息类型必须为数字',
  23. 'type.between' => '系统消息0,活动消息1',
  24. 'status.integer' => '查看状态必须为数字',
  25. 'status.between' => '查看状态:0未查看,1已查看',
  26. 'title.max' => '标题不能超过150个字符',
  27. 'des.required' => '描述必填',
  28. 'des.max' => '描述不能超过150个字符',
  29. 'litpic.max' => '缩略图不能超过100个字符',
  30. 'add_time.required' => '时间必填',
  31. 'add_time.integer' => '时间格式不正确',
  32. ];
  33. //场景验证规则
  34. protected $scene = [
  35. 'add' => ['user_id', 'type', 'title', 'des', 'litpic'],
  36. 'edit' => ['title', 'des'],
  37. 'del' => ['id'],
  38. ];
  39. /**
  40. * Determine if the user is authorized to make this request.
  41. *
  42. * @return bool
  43. */
  44. public function authorize()
  45. {
  46. return true; //修改为true
  47. }
  48. /**
  49. * Get the validation rules that apply to the request.
  50. *
  51. * @return array
  52. */
  53. public function rules()
  54. {
  55. return $this->rules;
  56. }
  57. /**
  58. * 获取被定义验证规则的错误消息.
  59. *
  60. * @return array
  61. */
  62. public function messages()
  63. {
  64. return $this->messages;
  65. }
  66. //获取场景验证规则
  67. public function getSceneRules($name, $fields = null)
  68. {
  69. $res = array();
  70. if(!isset($this->scene[$name]))
  71. {
  72. return false;
  73. }
  74. $scene = $this->scene[$name];
  75. if($fields != null && is_array($fields))
  76. {
  77. $scene = $fields;
  78. }
  79. foreach($scene as $k=>$v)
  80. {
  81. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  82. }
  83. return $res;
  84. }
  85. //获取场景验证规则自定义错误信息
  86. public function getSceneRulesMessages()
  87. {
  88. return $this->messages;
  89. }
  90. }