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.

104 lines
2.9 KiB

7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class KuaidiRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'name' => 'required|max:30',
  9. 'code' => 'required|max:20',
  10. 'money' => ['regex:/^\d{0,10}(\.\d{0,2})?$/'],
  11. 'country' => 'max:20',
  12. 'des' => 'max:150',
  13. 'tel' => 'max:60',
  14. 'website' => 'max:60',
  15. 'status' => 'integer|between:0,1',
  16. 'listorder' => 'integer',
  17. ];
  18. //总的自定义错误信息
  19. protected $messages = [
  20. 'id.required' => 'ID必填',
  21. 'id.integer' => 'ID必须为数字',
  22. 'name.required' => '快递公司名称必填',
  23. 'name.max' => '快递公司名称不能超过30个字符',
  24. 'code.required' => '快递公司编码必填',
  25. 'code.max' => '快递公司编码不能超过20个字符',
  26. 'money.regex' => '快递费只能带2位小数的数字',
  27. 'status.integer' => '状态必须是数字',
  28. 'status.between' => '状态 0显示',
  29. 'country.max' => '国家编码不能超过20个字符',
  30. 'des.max' => '说明不能超过150个字符',
  31. 'tel.max' => '电话不能超过60个字符',
  32. 'website.max' => '官网不能超过60个字符',
  33. 'listorder.integer' => '排序必须是数字',
  34. 'listorder.between' => '排序只能1-9999',
  35. ];
  36. //场景验证规则
  37. protected $scene = [
  38. 'add' => ['name', 'code', 'money', 'country', 'des', 'tel', 'website', 'status', 'listorder'],
  39. 'edit' => ['name', 'code', 'money', 'country', 'des', 'tel', 'website', 'status', 'listorder'],
  40. 'del' => ['id'],
  41. ];
  42. /**
  43. * Determine if the user is authorized to make this request.
  44. *
  45. * @return bool
  46. */
  47. public function authorize()
  48. {
  49. return true; //修改为true
  50. }
  51. /**
  52. * Get the validation rules that apply to the request.
  53. *
  54. * @return array
  55. */
  56. public function rules()
  57. {
  58. return $this->rules;
  59. }
  60. /**
  61. * 获取被定义验证规则的错误消息.
  62. *
  63. * @return array
  64. */
  65. public function messages()
  66. {
  67. return $this->messages;
  68. }
  69. //获取场景验证规则
  70. public function getSceneRules($name, $fields = null)
  71. {
  72. $res = array();
  73. if(!isset($this->scene[$name]))
  74. {
  75. return false;
  76. }
  77. $scene = $this->scene[$name];
  78. if($fields != null && is_array($fields))
  79. {
  80. $scene = $fields;
  81. }
  82. foreach($scene as $k=>$v)
  83. {
  84. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  85. }
  86. return $res;
  87. }
  88. //获取场景验证规则自定义错误信息
  89. public function getSceneRulesMessages()
  90. {
  91. return $this->messages;
  92. }
  93. }