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.

94 lines
2.4 KiB

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