Skip to content

Commit

Permalink
forgot to add the validator to the account service, just added it and…
Browse files Browse the repository at this point in the history
… used a method to render it more user firendly
  • Loading branch information
kiloutyg committed Feb 2, 2024
1 parent a6d1d04 commit 144a86d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

use App\Entity\User;

Expand All @@ -24,19 +25,22 @@ class AccountService
private $manager;
private $entityDeletionService;
private $logger;
private $validator;

public function __construct(
UserPasswordHasherInterface $passwordHasher,
UserRepository $userRepository,
EntityManagerInterface $manager,
EntityDeletionService $entityDeletionService,
LoggerInterface $logger
LoggerInterface $logger,
ValidatorInterface $validator
) {
$this->passwordHasher = $passwordHasher;
$this->userRepository = $userRepository;
$this->manager = $manager;
$this->entityDeletionService = $entityDeletionService;
$this->logger = $logger;
$this->validator = $validator;
}

// This function is responsible for creating a new user account and persisting it to the database
Expand Down Expand Up @@ -72,6 +76,25 @@ public function createAccount(Request $request)
$user->setUsername($name);
$user->setRoles([$role]);
$user->setEmailAddress($emailAddress);
$errors = $this->validator->validate($user);
if (count($errors) > 0) {
$errorMessages = [];
foreach ($errors as $violation) {
// You can use ->getPropertyPath() if you need to show the field name
// $errorMessages[] = $violation->getPropertyPath() . ': ' . $violation->getMessage();
$errorMessages[] = $violation->getMessage();
}

// Now you have an array of user-friendly messages you can display
// For example, you can separate them with new lines when displaying in text format:
$errorsString = implode("\n", $errorMessages);

// If you need to return JSON response:
// return new JsonResponse(['errors' => $errorMessages], Response::HTTP_BAD_REQUEST);

throw new \Exception($errorsString);
}

$this->manager->persist($user);
$this->manager->flush();

Expand Down

0 comments on commit 144a86d

Please sign in to comment.