Custom.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php namespace App\Http\Requests\Admin;
  2. use App\Http\Requests\BaseRequest;
  3. use Illuminate\Validation\Rule;
  4. /**
  5. * 客户验证器
  6. *
  7. */
  8. class Custom extends BaseRequest
  9. {
  10. /**
  11. * 获取应用于请求的规则
  12. *
  13. * @return array
  14. */
  15. public function rules()
  16. {
  17. // 编辑时排除ID
  18. $id = request('uid',null);
  19. // 非重规则phone
  20. $uniphone = Rule::unique('custom')->where(function ($query){
  21. return $query->where(['phone'=>request('phone','')]);
  22. })->ignore($id,'uid');
  23. // 返回结果
  24. return [
  25. // 有时候我们希望某个字段在第一次验证失败后就停止运行验证规则,只需要将 bail 添加到规则中:
  26. // 验证字段,验证规则,提示信息
  27. 'username' => ['required'],
  28. 'phone' => ['required','regex:/^1[3456789][0-9]{9}$/',$uniphone],
  29. 'uid' => 'required|integer|gt:0',
  30. 'custom_file' => 'required|file'
  31. ];
  32. }
  33. // 场景列表
  34. protected $scenes = [
  35. 'add' => ['username','phone'],
  36. 'edit' => ['uid','username','phone'],
  37. 'set_status' => ['uid'],
  38. 'import_execl' => ['order_file'],
  39. ];
  40. /**
  41. * 获取已定义验证规则的错误消息
  42. *
  43. * @return array
  44. */
  45. public function messages()
  46. {
  47. return [
  48. 'username.required' => '客户名称必填',
  49. 'username.unique' => '客户名称已经存在',
  50. 'phone.required' => '联系方式必填',
  51. 'phone.regex' => '联系方式格式错误',
  52. 'phone.unique' => '联系方式已经存在',
  53. 'uid.required' => 'ID未知',
  54. 'uid.integer' => 'ID格式错误',
  55. 'uid.gt' => 'ID格式错误',
  56. 'import_execl.required'=> '请上传Excel',
  57. 'import_execl.file' => '请上传Excel文件',
  58. ];
  59. }
  60. }