StoreUser.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Http\Requests\User;
  3. use App\Models\Role;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. class StoreUser extends FormRequest
  6. {
  7. /**
  8. * Determine if the user is authorized to make this request.
  9. */
  10. public function authorize(): bool
  11. {
  12. return hasRole('admin');
  13. }
  14. /**
  15. * Get the validation rules that apply to the request.
  16. *
  17. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  18. */
  19. public function rules(): array
  20. {
  21. return [
  22. 'id' => 'nullable|exists:users',
  23. 'email' => 'required_if:id,null|email|unique:users,email,'.$this->id,
  24. 'name' => 'required|string|min:2',
  25. 'password' => 'required_without:id|nullable|string|min:4',
  26. 'role' => 'nullable|string|in:' . implode(',', Role::VALID_ROLES),
  27. ];
  28. }
  29. public function messages(): array
  30. {
  31. return [
  32. 'name' => 'Поле обязательно для заполнения и должно быть не менее 2 символов!',
  33. 'password' => 'Пароль должен быть не менее 8 символов!',
  34. 'role' => 'Неправильная роль!',
  35. 'email' => 'Нужно указать правильный адрес почты, который не зарегистрирован в системе!',
  36. ];
  37. }
  38. }