- Le pseudo et le mail est maintenant unique.

This commit is contained in:
2025-01-30 15:30:49 +01:00
parent 4a84f5ec7d
commit c93bfd585e
5 changed files with 156 additions and 13 deletions

View File

@ -9,16 +9,19 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class RegistrationController extends AbstractController
{
private $entityManager;
private $passwordHasher;
private $validator;
public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordHasher)
public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordHasher, ValidatorInterface $validator)
{
$this->entityManager = $entityManager;
$this->passwordHasher = $passwordHasher;
$this->validator = $validator;
}
#[Route('/registration', name: 'app_registration')]
@ -28,7 +31,18 @@ class RegistrationController extends AbstractController
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->isSubmitted()) {
$existingEmail = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
$existingPseudo = $this->entityManager->getRepository(User::class)->findOneBy(['pseudo' => $user->getPseudo()]);
if ($existingEmail) {
$form->get('email')->addError(new \Symfony\Component\Form\FormError('Cet email est déjà utilisé.'));
}
if ($existingPseudo) {
$form->get('pseudo')->addError(new \Symfony\Component\Form\FormError('Ce pseudo est déjà pris.'));
}
$plainPassword = $user->getPassword();
$confirmPassword = $form->get('confirmPassword')->getData();
@ -37,14 +51,23 @@ class RegistrationController extends AbstractController
return $this->redirectToRoute('app_registration');
}
$hashedPassword = $this->passwordHasher->hashPassword($user, $plainPassword);
$user->setPassword($hashedPassword);
$errors = $this->validator->validate($user);
if (count($errors) > 0) {
foreach ($errors as $error) {
$form->addError(new \Symfony\Component\Form\FormError($error->getMessage()));
}
}
$this->entityManager->persist($user);
$this->entityManager->flush();
if ($form->isValid()) {
$hashedPassword = $this->passwordHasher->hashPassword($user, $plainPassword);
$user->setPassword($hashedPassword);
$this->addFlash('success', 'Votre compte a été créé avec succès !');
return $this->redirectToRoute('home');
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->addFlash('success', 'Votre compte a été créé avec succès !');
return $this->redirectToRoute('home');
}
}
return $this->render('registration/index.html.twig', [