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
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class GoodsImgRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'url' => 'required|max:150',
  9. 'goods_id' => 'required|integer',
  10. 'add_time' => 'required|integer',
  11. 'des' => 'max:150',
  12. 'listorder' => 'integer|between:1,9999',
  13. ];
  14. //总的自定义错误信息
  15. protected $messages = [
  16. 'id.required' => 'ID必填',
  17. 'id.integer' => 'ID必须为数字',
  18. 'url.required' => '图片地址必填',
  19. 'url.max' => '图片地址不能超过150个字',
  20. 'goods_id.required' => '商品id必填',
  21. 'goods_id.integer' => '商品id必须为数字',
  22. 'add_time.required' => '添加时间必填',
  23. 'add_time.integer' => '添加时间必须是数字',
  24. 'des.max' => '图片说明信息不能超过150个字',
  25. 'listorder.integer' => '排序必须是数字',
  26. 'listorder.between' => '排序只能1-9999',
  27. ];
  28. //场景验证规则
  29. protected $scene = [
  30. 'add' => ['url', 'goods_id', 'add_time', 'des', 'listorder'],
  31. 'edit' => ['url', 'goods_id', 'add_time', 'des', 'listorder'],
  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. }