AgentUserSaveRequest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Requests\Sell;
  3. use App\Http\Requests\Request;
  4. use Illuminate\Database\Query\Builder;
  5. use Illuminate\Validation\Rule;
  6. class AgentUserSaveRequest extends Request
  7. {
  8. /**
  9. * Determine if the user is authorized to make this request.
  10. *
  11. * @return bool
  12. */
  13. public function authorize()
  14. {
  15. return true;
  16. }
  17. /**
  18. * Get the validation rules that apply to the request.
  19. *
  20. * @return array
  21. */
  22. public function rules()
  23. {
  24. if (!$this->ajax()) {
  25. return [];
  26. }
  27. $userId = $this->route('id');
  28. if ($userId) {
  29. $password = 'min:5|max:15';
  30. } else {
  31. $password = 'nullable|min:5|max:15';
  32. }
  33. $rule = [
  34. 'nickname' => 'required',
  35. 'username' => [
  36. 'required',
  37. 'min:3',
  38. 'max:15',
  39. Rule::unique('users')->where(function (Builder $query) {
  40. return $query->where('deleted_at', null);
  41. })->ignore($userId)
  42. ],
  43. 'password' => $password,
  44. 'phone' => [
  45. 'required',
  46. Rule::unique('users')->where(function (Builder $query) {
  47. return $query->where('deleted_at', null);
  48. })->ignore($userId)
  49. ],
  50. 'telephone' => 'nullable',
  51. 'email' => 'nullable|email',
  52. 'role_id' => 'required|numeric',
  53. 'agent_id' => 'required'
  54. ];
  55. return $rule;
  56. }
  57. public function attributes()
  58. {
  59. return [
  60. 'nickname' => '昵称',
  61. 'username' => '用户名',
  62. 'password' => '密码',
  63. 'phone' => "手机号",
  64. 'role_id' => '角色',
  65. 'agent_id' => '代理商'
  66. ];
  67. }
  68. }