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.

96 lines
2.4 KiB

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