StoreUser.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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
  18. */
  19. public function rules(): array
  20. {
  21. return [
  22. 'id' => 'nullable|exists:users',
  23. 'email' => 'required_if:id,null|unique:users,email,'.$this->id,
  24. 'name' => 'required|string|min:2',
  25. 'phone' => 'nullable|string',
  26. 'password' => 'required_without:id|nullable|string|min:4',
  27. 'role' => 'nullable|string|in:' . implode(',', Role::VALID_ROLES),
  28. 'color' => 'nullable|string',
  29. ];
  30. }
  31. public function messages(): array
  32. {
  33. return [
  34. 'name' => 'Поле обязательно для заполнения и должно быть не менее 2 символов!',
  35. 'password' => 'Пароль должен быть не менее 8 символов!',
  36. 'role' => 'Неправильная роль!',
  37. 'email' => 'Нужно указать адрес почты или логин, который не зарегистрирован в системе!',
  38. ];
  39. }
  40. }