UpdateRequest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Requests\API\User;
  3. use App\Http\Requests\RuleHelper;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Hash;
  7. class UpdateRequest extends FormRequest
  8. {
  9. use RuleHelper;
  10. /**
  11. * Determine if the user is authorized to make this request.
  12. */
  13. public function authorize(): bool
  14. {
  15. return true;
  16. }
  17. /**
  18. * Get the validation rules that apply to the request.
  19. *
  20. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  21. */
  22. public function rules(): array
  23. {
  24. return [
  25. 'name' => 'required|max:100',
  26. 'email' => 'required|email',//为了编辑时没改邮箱的情况不用required|email|unique:users
  27. 'password' => 'nullable|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,}$/', // 编辑时可为空,包含大小写字母和数字,At least 6 digits, including upper and lower case letters and numbers
  28. 'auth_password' => [
  29. 'required',
  30. function ($attribute, $value, $fail) {
  31. if (!Hash::check($value, Auth::user()->password)) {
  32. $fail("Wrong security authentication password!");
  33. }
  34. }
  35. ],
  36. 'phone'=>'nullable|regex:/^\d{8,11}$/',
  37. 'gender'=>'nullable|in:1,0',
  38. 'address'=>'nullable|max:255',
  39. 'company_id'=>'exists:company,id',
  40. 'department_id'=>'exists:department,id',
  41. 'role_id'=>'required|exists:roles,id',
  42. ];
  43. }
  44. }