CustomAddr.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php namespace App\Http\Requests\Api;
  2. use App\Http\Requests\BaseRequest;
  3. /**
  4. * 地址验证器
  5. *
  6. */
  7. class CustomAddr extends BaseRequest
  8. {
  9. /**
  10. * 获取应用于请求的规则
  11. *
  12. * @return array
  13. */
  14. public function rules()
  15. {
  16. // 编辑时排除ID
  17. // 返回结果
  18. return [
  19. // 有时候我们希望某个字段在第一次验证失败后就停止运行验证规则,只需要将 bail 添加到规则中:
  20. // 验证字段,验证规则,提示信息
  21. // 地址
  22. 'id' => 'required|integer|gt:0',
  23. 'contact_province' => 'required|max:15',
  24. 'contact_city' => 'required|max:15',
  25. 'contact_area' => 'required|max:15',
  26. 'contact_addr' => 'required|max:64',
  27. // 备注
  28. 'contact_name' => 'required|max:20',
  29. 'contact_shop' => 'required|max:20',
  30. 'contact_phone' => 'required|phone',
  31. ];
  32. }
  33. // 场景列表
  34. protected $scenes = [
  35. 'add' => ['contact_province','contact_city','contact_area','contact_name','contact_phone','contact_shop'],
  36. 'edit' => ['id','contact_province','contact_city','contact_area','contact_name','contact_phone','contact_shop'],
  37. 'del' => ['id'],
  38. 'set_default' => ['id'],
  39. 'get_guess_addr' => ['contact_shop'],
  40. ];
  41. /**
  42. * 获取已定义验证规则的错误消息
  43. *
  44. * @return array
  45. */
  46. public function messages()
  47. {
  48. return [
  49. 'contact_province.required' => '请选择地区',
  50. 'contact_city.required' => '请选择地区',
  51. 'contact_area.required' => '请选择地区',
  52. 'contact_addr.required' => '请填写地址',
  53. 'contact_province.max' => '省份超长啦',
  54. 'contact_city.max' => '城市超长啦',
  55. 'contact_area.max' => '区县超长啦',
  56. 'contact_addr.max' => '地址超长啦',
  57. 'contact_name.required' => '请填写收件人',
  58. 'contact_shop.required' => '请填写店铺名称',
  59. 'contact_shop.max' => '店铺最多20字',
  60. 'contact_phone.required' => '请填写收件人手机号',
  61. 'contact_phone.phone' => '请填写正确的手机号',
  62. 'id.required' => '请选择地址',
  63. 'id.integer' => '地址格式有误',
  64. 'id.gt' => '地址格式有误',
  65. ];
  66. }
  67. }