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.7 KiB

  1. <?php
  2. namespace App\Http\Requests;
  3. class AdRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'name' => 'required|max:60',
  9. 'description' => 'max:255',
  10. 'flag' => 'max:30',
  11. 'is_expire' => 'between:0,3',
  12. 'start_time' => 'integer',
  13. 'end_time' => 'integer|min:start_time',
  14. 'add_time' => 'required|integer',
  15. ];
  16. //总的自定义错误信息
  17. protected $messages = [
  18. 'id.required' => 'ID不能为空',
  19. 'id.integer' => 'ID必须是数字',
  20. 'id.gt' => 'ID格式不正确',
  21. 'name.required' => '名称不能为空',
  22. 'name.max' => '名称不能超过60个字符',
  23. 'description.max' => '描述不能超过255个字符',
  24. 'flag.max' => '标识不能超过30个字符',
  25. 'is_expire.between' => '0永不过期',
  26. 'start_time.integer' => '投放开始时间格式不正确',
  27. 'end_time.integer' => '投放结束时间格式不正确',
  28. 'end_time.min' => '投放结束时间格式不正确',
  29. 'add_time.required' => '添加时间不能为空',
  30. 'add_time.integer' => '添加时间格式不正确',
  31. 'add_time.gt' => '添加时间格式不正确',
  32. ];
  33. //场景验证规则
  34. protected $scene = [
  35. 'add' => ['name', 'description', 'flag', 'is_expire', 'start_time', 'end_time', 'add_time'],
  36. 'edit' => ['name', 'description', 'flag', 'is_expire', 'start_time', 'end_time', 'add_time'],
  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. }