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.

91 lines
2.1 KiB

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