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.

110 lines
3.5 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class BonusRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'name' => 'required|max:60',
  9. 'money' => ['required','regex:/^\d{0,10}(\.\d{0,2})?$/'],
  10. 'min_amount' => ['required','regex:/^\d{0,10}(\.\d{0,2})?$/'],
  11. 'start_time' => 'required|date_format:"Y-m-d H:i:s"',
  12. 'end_time' => 'required|date_format:"Y-m-d H:i:s"|after:start_time',
  13. 'point' => 'integer|between:1,9999',
  14. 'status' => 'integer|between:0,1',
  15. 'add_time' => 'required|integer',
  16. 'num' => 'integer|between:-1,999999',
  17. ];
  18. //总的自定义错误信息
  19. protected $messages = [
  20. 'id.required' => 'ID必填',
  21. 'id.integer' => 'ID必须为数字',
  22. 'name.required' => '名称必填',
  23. 'name.max' => '名称不能大于60个字',
  24. 'money.required' => '金额必填',
  25. 'money.regex' => '金额只能带2位小数的数字',
  26. 'min_amount.required' => '满多少使用必填',
  27. 'min_amount.regex' => '满多少使用只能带2位小数的数字',
  28. 'start_time.required' => '开始时间必填',
  29. 'start_time.date_format' => '开始时间格式不正确,格式:1990-01-01 00:00:00',
  30. 'end_time.required' => '结束时间必填',
  31. 'end_time.date_format' => '结束时间格式不正确,格式:1990-01-01 00:00:00',
  32. 'end_time.after' => '结束时间必须大于开始时间',
  33. 'point.integer' => '兑换优惠券所需积分必须是数字',
  34. 'point.between' => '兑换优惠券所需积分只能1-9999',
  35. 'status.integer' => '兑换优惠券所需积分必须是数字',
  36. 'status.between' => '状态:0可用,1不可用',
  37. 'add_time.required' => '添加时间必填',
  38. 'add_time.integer' => '添加时间必须是数字',
  39. 'num.integer' => '优惠券数量必须是数字',
  40. 'num.between' => '优惠券数量只能-1-999999',
  41. ];
  42. //场景验证规则
  43. protected $scene = [
  44. 'add' => ['name', 'money', 'min_amount', 'start_time', 'end_time', 'point', 'status', 'add_time', 'num'],
  45. 'edit' => ['name', 'money', 'min_amount', 'start_time', 'end_time', 'point', 'status', 'add_time', 'num'],
  46. 'del' => ['id'],
  47. ];
  48. /**
  49. * Determine if the user is authorized to make this request.
  50. *
  51. * @return bool
  52. */
  53. public function authorize()
  54. {
  55. return true; //修改为true
  56. }
  57. /**
  58. * Get the validation rules that apply to the request.
  59. *
  60. * @return array
  61. */
  62. public function rules()
  63. {
  64. return $this->rules;
  65. }
  66. /**
  67. * 获取被定义验证规则的错误消息.
  68. *
  69. * @return array
  70. */
  71. public function messages()
  72. {
  73. return $this->messages;
  74. }
  75. //获取场景验证规则
  76. public function getSceneRules($name, $fields = null)
  77. {
  78. $res = array();
  79. if(!isset($this->scene[$name]))
  80. {
  81. return false;
  82. }
  83. $scene = $this->scene[$name];
  84. if($fields != null && is_array($fields))
  85. {
  86. $scene = $fields;
  87. }
  88. foreach($scene as $k=>$v)
  89. {
  90. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  91. }
  92. return $res;
  93. }
  94. //获取场景验证规则自定义错误信息
  95. public function getSceneRulesMessages()
  96. {
  97. return $this->messages;
  98. }
  99. }