CreateRequest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 CreateRequest 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|unique:users',
  27. 'username'=>'required|max:30|unique:users',
  28. 'password' => 'required|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,}$/', // 至少6位,包含大小写字母和数字,At least 6 digits, including upper and lower case letters and numbers
  29. 'phone'=>'nullable|regex:/^\d{8,11}$/',
  30. 'gender'=>'nullable|in:1,0',
  31. 'address'=>'nullable|max:255',
  32. 'company_id'=>'required|exists:company,id',
  33. 'department_id'=>'required|exists:department,id',
  34. 'role_id'=>'required|exists:roles,id',
  35. 'auth_password' => [
  36. 'required',
  37. function ($attribute, $value, $fail) {
  38. if (!Hash::check($value, Auth::user()->password)) {
  39. $fail("Wrong security authentication password!");
  40. }
  41. }
  42. ],
  43. ];
  44. }
  45. }