Custom.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. 'set_manager' => ['uid'],
  39. 'import_execl' => ['custom_file'],
  40. ];
  41. /**
  42. * 获取已定义验证规则的错误消息
  43. *
  44. * @return array
  45. */
  46. public function messages()
  47. {
  48. return [
  49. 'username.required' => '客户名称必填',
  50. 'username.unique' => '客户名称已经存在',
  51. 'phone.required' => '联系方式必填',
  52. 'phone.regex' => '联系方式格式错误',
  53. 'phone.unique' => '联系方式已经存在',
  54. 'uid.required' => 'ID未知',
  55. 'uid.integer' => 'ID格式错误',
  56. 'uid.gt' => 'ID格式错误',
  57. 'import_execl.required'=> '请上传Excel',
  58. 'import_execl.file' => '请上传Excel文件',
  59. ];
  60. }
  61. }