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.

114 lines
3.4 KiB

6 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class UserAddressRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'user_id' => 'required|integer',
  9. 'name' => 'required|max:60',
  10. 'country' => 'required|integer',
  11. 'province' => 'required|integer',
  12. 'city' => 'required|integer',
  13. 'district' => 'required|integer',
  14. 'address' => 'required|max:120',
  15. 'zipcode' => 'integer',
  16. 'telphone' => 'max:60',
  17. 'mobile' => 'required|max:60',
  18. 'is_default' => 'required|integer|between:0,1',
  19. ];
  20. //总的自定义错误信息
  21. protected $messages = [
  22. 'id.required' => 'ID必填',
  23. 'id.integer' => 'ID必须为数字',
  24. 'user_id.required' => '用户ID必填',
  25. 'user_id.integer' => '用户ID必须为数字',
  26. 'name.required' => '收货人名字必填',
  27. 'name.max' => '收货人名字不能超过60个字符',
  28. 'country.required' => '国家ID必填',
  29. 'country.integer' => '国家ID必须为数字',
  30. 'province.required' => '省份ID必填',
  31. 'province.integer' => '省份ID必须为数字',
  32. 'city.required' => '城市ID必填',
  33. 'city.integer' => '城市ID必须为数字',
  34. 'district.required' => '地区ID必填',
  35. 'district.integer' => '地区ID必须为数字',
  36. 'address.required' => '收货人的详细地址必填',
  37. 'address.max' => '收货人的详细地址不能超过120个字符',
  38. 'zipcode.integer' => '收货人邮编必须为数字',
  39. 'telphone.max' => '固定电话不能超过60个字符',
  40. 'mobile.required' => '收货人手机号必填',
  41. 'mobile.max' => '收货人手机号不能超过20个字符',
  42. 'is_default.required' => '是否默认必填',
  43. 'is_default.integer' => '是否默认必须是数字',
  44. 'is_default.between' => '是否默认,0:为非默认,1:默认',
  45. ];
  46. //场景验证规则
  47. protected $scene = [
  48. 'add' => ['user_id', 'name', 'province', 'city', 'district', 'address', 'zipcode', 'telphone', 'mobile', 'is_default'],
  49. 'edit' => ['zipcode', 'telphone', 'is_default'],
  50. 'del' => ['user_id', 'id'],
  51. ];
  52. /**
  53. * Determine if the user is authorized to make this request.
  54. *
  55. * @return bool
  56. */
  57. public function authorize()
  58. {
  59. return true; //修改为true
  60. }
  61. /**
  62. * Get the validation rules that apply to the request.
  63. *
  64. * @return array
  65. */
  66. public function rules()
  67. {
  68. return $this->rules;
  69. }
  70. /**
  71. * 获取被定义验证规则的错误消息.
  72. *
  73. * @return array
  74. */
  75. public function messages()
  76. {
  77. return $this->messages;
  78. }
  79. //获取场景验证规则
  80. public function getSceneRules($name, $fields = null)
  81. {
  82. $res = array();
  83. if(!isset($this->scene[$name]))
  84. {
  85. return false;
  86. }
  87. $scene = $this->scene[$name];
  88. if($fields != null && is_array($fields))
  89. {
  90. $scene = $fields;
  91. }
  92. foreach($scene as $k=>$v)
  93. {
  94. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  95. }
  96. return $res;
  97. }
  98. //获取场景验证规则自定义错误信息
  99. public function getSceneRulesMessages()
  100. {
  101. return $this->messages;
  102. }
  103. }