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. // 编辑时排除ID
  20. // 非重规则
  21. $unique = Rule::unique('custom')->where(function ($query) {
  22. return $query->where(['username'=>request('username','')]);
  23. })->ignore($id,'uid');
  24. // 非重规则phone
  25. $uniphone = Rule::unique('custom')->where(function ($query){
  26. return $query->where(['phone'=>request('phone','')]);
  27. })->ignore($id,'uid');
  28. // 返回结果
  29. return [
  30. // 有时候我们希望某个字段在第一次验证失败后就停止运行验证规则,只需要将 bail 添加到规则中:
  31. // 验证字段,验证规则,提示信息
  32. 'username' => ['required',$unique],
  33. 'phone' => ['required','regex:/^1[3456789][0-9]{9}$/',$uniphone],
  34. 'uid' => 'required|integer|gt:0',
  35. ];
  36. }
  37. // 场景列表
  38. protected $scenes = [
  39. 'add' => ['username','phone'],
  40. 'edit' => ['uid','username','phone'],
  41. 'set_status' => ['uid'],
  42. ];
  43. /**
  44. * 获取已定义验证规则的错误消息
  45. *
  46. * @return array
  47. */
  48. public function messages()
  49. {
  50. return [
  51. 'username.required' => '客户名称必填',
  52. 'username.unique' => '客户名称已经存在',
  53. 'phone.required' => '联系方式必填',
  54. 'phone.regex' => '联系方式格式错误',
  55. 'phone.unique' => '联系方式已经存在',
  56. 'uid.required' => 'ID未知',
  57. 'uid.integer' => 'ID格式错误',
  58. 'uid.gt' => 'ID格式错误',
  59. ];
  60. }
  61. }