src/Form/RegistrationFormType.php line 20

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\UserEntity;
  4. use Symfony\Component\Form\Extension\Core\Type\TextType;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Component\Validator\Constraints\Length;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Component\Validator\Constraints\Email;
  14. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\Validator\Constraints\Callback;
  17. class RegistrationFormType extends AbstractType
  18. {   
  19.     private $translator;
  20.     private $entityManager;
  21.     public function __construct(TranslatorInterface $translatorEntityManagerInterface $entityManager)
  22.     {
  23.         $this->translator $translator;
  24.         $this->entityManager $entityManager;
  25.     }
  26.     public function buildForm(FormBuilderInterface $builder, array $options): void
  27.     {
  28.         $builder
  29.             ->add('username'TextType::class,                 
  30.                 [
  31.                     'label' =>  false,
  32.                     'attr' =>  ['class' => 'w-100 mb-2 p-2 border border-gray-300 rounded-md''placeholder' => $this->translator->trans('login_username_placeholder')],
  33.                 ])
  34.             ->add('email'EmailType::class, [
  35.                 'label' =>  false,
  36.                 'attr' => ['class' => 'w-100 mb-2 p-2 border border-gray-300 rounded-md''placeholder' => $this->translator->trans('login_email_placeholder')],
  37.                 'constraints' => [
  38.                     new NotBlank([
  39.                         'message' => $this->translator->trans('email_unformated'),
  40.                     ]),
  41.                     new Email([
  42.                         'message' => $this->translator->trans('email_unformated'),
  43.                     ]),
  44.                 ]
  45.             ])
  46.             ->add('plainPassword'PasswordType::class, [
  47.                 'mapped' => false,
  48.                 'label' =>  false,
  49.                 'attr' => ['autocomplete' => 'new-password''class' => 'w-100 mb-2 p-2 border border-gray-300 rounded-md''placeholder' => $this->translator->trans('login_password_placeholder')], 
  50.                 'constraints' => [
  51.                     new NotBlank([
  52.                         'message' => $this->translator->trans('password_unformated'),
  53.                     ]),
  54.                     new Length([
  55.                         'min' => 8,
  56.                         'minMessage' => $this->translator->trans('password_unformated'),
  57.                         'max' => 4096,
  58.                     ]),
  59.                 ],
  60.             ])
  61.         ;
  62.     }
  63.     public function configureOptions(OptionsResolver $resolver): void
  64.     {
  65.         $resolver->setDefaults([
  66.             'data_class' => UserEntity::class,
  67.             'constraints' => [
  68.                 new Callback([$this'validateUniqueEmail']),
  69.             ],
  70.         ]);
  71.     }
  72.     public function validateUniqueEmail($objectExecutionContextInterface $context)
  73.     {
  74.         $existingUser $this->entityManager->getRepository(UserEntity::class)->findOneBy(['email' => $object->getEmail()]);
  75.         if ($existingUser) {
  76.             $context->buildViolation($this->translator->trans('email_exists'))
  77.                 ->atPath('email')
  78.                 ->addViolation();
  79.         }
  80.     }
  81. }