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.

100 lines
2.7 KiB

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