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.

154 lines
6.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class GoodsRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'title' => 'required|max:150',
  9. 'typeid' => 'required|integer',
  10. 'click' => 'integer',
  11. 'tuijian' => 'integer',
  12. 'sn' => 'max:60',
  13. 'price' => ['required','regex:/^\d{0,10}(\.\d{0,2})?$/'],
  14. 'litpic' => 'required|max:100',
  15. 'pubdate' => 'integer',
  16. 'add_time' => 'required|integer',
  17. 'keywords' => 'max:60',
  18. 'seotitle' => 'max:150',
  19. 'description' => 'max:250',
  20. 'status' => 'integer|between:0,5',
  21. 'shipping_fee' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'],
  22. 'market_price' => ['required','regex:/^\d{0,10}(\.\d{0,2})?$/'],
  23. 'goods_number' => 'required|integer|between:1,99999',
  24. 'user_id' => 'integer',
  25. 'sale' => 'integer|between:1,99999',
  26. 'cost_price' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'],
  27. 'goods_weight' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'],
  28. 'point' => 'integer|between:1,99999',
  29. 'comments' => 'integer|between:1,99999',
  30. 'promote_start_date' => 'integer',
  31. 'promote_end_date' => 'integer',
  32. 'promote_price' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'],
  33. 'goods_img' => 'max:250',
  34. 'warn_number' => 'integer|between:1,99',
  35. 'listorder' => 'integer|between:1,9999',
  36. 'brand_id' => 'integer',
  37. ];
  38. //总的自定义错误信息
  39. protected $messages = [
  40. 'id.required' => 'ID必填',
  41. 'id.integer' => 'ID必须为数字',
  42. 'title.required' => '标题必填',
  43. 'title.max' => '标题不能超过150个字符',
  44. 'typeid.required' => '栏目ID必填',
  45. 'typeid.integer' => '栏目ID必须为数字',
  46. 'click.integer' => '点击量必须为数字',
  47. 'tuijian.integer' => '推荐等级必须是数字',
  48. 'sn.max' => '货号不能超过60个字符',
  49. 'price.required' => '产品价格必填',
  50. 'price.regex' => '产品价格只能带2位小数的数字',
  51. 'litpic.required' => '缩略图必须上传',
  52. 'litpic.max' => '缩略图不能超过100个字符',
  53. 'pubdate.integer' => '更新时间格式不正确',
  54. 'add_time.required' => '添加时间必填',
  55. 'add_time.integer' => '添加时间必须是数字',
  56. 'keywords.max' => '关键词不能超过60个字符',
  57. 'seotitle.max' => 'seo标题不能超过150个字符',
  58. 'description.max' => '描述不能超过250个字符',
  59. 'status.integer' => '商品状态必须是数字',
  60. 'status.between' => '商品状态 0正常 1已删除 2下架 3申请上架',
  61. 'shipping_fee.regex' => '运费格式不正确,运费只能带2位小数的数字',
  62. 'market_price.required' => '市场价格必填',
  63. 'market_price.regex' => '市场价格格式不正确,市场价格只能带2位小数的数字',
  64. 'goods_number.required' => '库存必填',
  65. 'goods_number.integer' => '库存必须是数字',
  66. 'goods_number.between' => '库存只能1-99999',
  67. 'user_id.integer' => '发布者ID必须是数字',
  68. 'sale.integer' => '销量必须是数字',
  69. 'sale.between' => '销量只能1-99999',
  70. 'cost_price.regex' => '成本价格格式不正确,成本价格只能带2位小数的数字',
  71. 'goods_weight.regex' => '重量格式不正确,重量只能带2位小数的数字',
  72. 'point.integer' => '购买该商品时每笔成功交易赠送的积分数量必须是数字',
  73. 'point.between' => '购买该商品时每笔成功交易赠送的积分数量只能1-99999',
  74. 'comments.integer' => '评论次数必须是数字',
  75. 'comments.between' => '评论次数只能1-9999999',
  76. 'promote_start_date.integer' => '促销价格开始日期必须是数字',
  77. 'promote_end_date.integer' => '促销价格结束日期必须是数字',
  78. 'promote_price.regex' => '促销价格格式不正确,促销价格只能带2位小数的数字',
  79. 'goods_img.max' => '商品的实际大小图片不能超过250个字符',
  80. 'warn_number.integer' => '商品报警数量必须是数字',
  81. 'warn_number.between' => '商品报警数量只能1-99',
  82. 'listorder.integer' => '排序必须是数字',
  83. 'listorder.between' => '排序只能1-9999',
  84. 'brand_id.integer' => '商品品牌ID必须是数字',
  85. ];
  86. //场景验证规则
  87. protected $scene = [
  88. 'add' => ['typeid', 'title', 'tuijian', 'click', 'sn', 'price', 'litpic', 'pubdate', 'add_time', 'keywords', 'seotitle', 'description', 'status', 'shipping_fee', 'market_price', 'goods_number', 'user_id', 'sale', 'cost_price', 'goods_weight', 'point', 'comments', 'promote_start_date', 'promote_price', 'promote_end_date', 'goods_img', 'warn_number', 'listorder', 'brand_id'],
  89. 'edit' => ['typeid', 'title', 'tuijian', 'click', 'sn', 'price', 'litpic', 'pubdate', 'add_time', 'keywords', 'seotitle', 'description', 'status', 'shipping_fee', 'market_price', 'goods_number', 'user_id', 'sale', 'cost_price', 'goods_weight', 'point', 'comments', 'promote_start_date', 'promote_price', 'promote_end_date', 'goods_img', 'warn_number', 'listorder', 'brand_id'],
  90. 'del' => ['id'],
  91. ];
  92. /**
  93. * Determine if the user is authorized to make this request.
  94. *
  95. * @return bool
  96. */
  97. public function authorize()
  98. {
  99. return true; //修改为true
  100. }
  101. /**
  102. * Get the validation rules that apply to the request.
  103. *
  104. * @return array
  105. */
  106. public function rules()
  107. {
  108. return $this->rules;
  109. }
  110. /**
  111. * 获取被定义验证规则的错误消息.
  112. *
  113. * @return array
  114. */
  115. public function messages()
  116. {
  117. return $this->messages;
  118. }
  119. //获取场景验证规则
  120. public function getSceneRules($name, $fields = null)
  121. {
  122. $res = array();
  123. if(!isset($this->scene[$name]))
  124. {
  125. return false;
  126. }
  127. $scene = $this->scene[$name];
  128. if($fields != null && is_array($fields))
  129. {
  130. $scene = $fields;
  131. }
  132. foreach($scene as $k=>$v)
  133. {
  134. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  135. }
  136. return $res;
  137. }
  138. //获取场景验证规则自定义错误信息
  139. public function getSceneRulesMessages()
  140. {
  141. return $this->messages;
  142. }
  143. }