src/Form/RegistrationSimpleFormType.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Option;
  4. use App\Entity\User;
  5. use App\Form\MediaVichType;
  6. use App\Repository\OptionRepository;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  10. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Validator\Constraints\IsTrue;
  19. use Symfony\Component\Validator\Constraints\NotBlank;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
  22. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  23. use Symfony\Component\Form\FormError;
  24. class RegistrationSimpleFormType extends AbstractType
  25. {
  26.     private $logger;
  27.     private $optionRepository;
  28.     private $translator;
  29.     public function __construct(
  30.         LoggerInterface $logger,
  31.         OptionRepository $optionRepository,
  32.         TranslatorInterface $translator
  33.     ) {
  34.         $this->logger $logger;
  35.         $this->optionRepository $optionRepository;
  36.         $this->translator $translator;
  37.     }
  38.     public function buildForm(FormBuilderInterface $builder, array $options): void
  39.     {
  40.         $policyTermsUrl $this->optionRepository->getValueByKey(Option::URL_POLICY_TERMS"#");
  41.         $useConditionsUrl $this->optionRepository->getValueByKey(Option::URL_USE_CONDITION"#");
  42.         $builder
  43.             ->add('nickname'TextType::class, [
  44.                 'label' => false,
  45.                 'attr' => [
  46.                     'placeholder' => 'general.nickname',
  47.                     'class' => 'form-control required-placeholder'
  48.                 ]
  49.             ])
  50.             ->add('email'TextType::class, [
  51.                 'label' => false,
  52.                 'attr' => [
  53.                     'placeholder' => 'general.email',
  54.                     'class' => 'form-control required-placeholder'
  55.                 ]
  56.             ])
  57.             ->add('cpostal'TextType::class, [
  58.                 'label' => false,
  59.                 'attr' => [
  60.                     'placeholder' => 'general.cpostal',
  61.                     'class' => 'form-control required-placeholder'
  62.                 ]
  63.             ])
  64.             ->add('agreeTerms'CheckboxType::class, [
  65.                 'label_html' => true,
  66.                 'label' => $this->translator->trans('general.agree_terms', ['%useConditionsUrl%' => $useConditionsUrl'%policyTermsUrl%' => $policyTermsUrl], 'messages') . ' *',
  67.                 'mapped' => false,
  68.                 'row_attr' => [
  69.                     'class' => 'checkbox-inline',
  70.                 ],
  71.                 'required' => true,
  72.                 'constraints' => [
  73.                     new IsTrue([
  74.                         'message' => $this->translator->trans('general.shouldAgreeTerms'),
  75.                     ]),
  76.                 ],
  77.             ])
  78.             ->add('legalAge'CheckboxType::class, [
  79.                 'label' =>  $this->translator->trans('login.legalAge'). ' *',
  80.                 'required' => true,
  81.                 'data' => false
  82.             ])
  83.             ->add('username'HiddenType::class, [])
  84.             ->add('recaptchaToken'HiddenType::class, [
  85.                 'mapped' => false,
  86.             ]);
  87.         $builder
  88.             ->addEventListener(
  89.                 FormEvents::PRE_SET_DATA,
  90.                 [$this'onPreSetData']
  91.             )
  92.             ->addEventListener(
  93.                 FormEvents::PRE_SUBMIT,
  94.                 [$this'onPreSubmit']
  95.             )
  96.             ->addEventListener(
  97.                 FormEvents::POST_SUBMIT,
  98.                 [$this'onPostSubmit']
  99.             );
  100.     }
  101.     public function onPreSetData(FormEvent $event): void
  102.     {
  103.         $user $event->getData();
  104.         $form $event->getForm();
  105.         $minLength $this->optionRepository->getValueByKey(Option::PASSWORD_MINIMUM_LENGTH8);
  106.         $minStrength $this->optionRepository->getValueByKey(Option::PASSWORD_MINIMUM_STRENGTH3);
  107.         $passwordConstraints = [
  108.             new RollerworksPassword\PasswordStrength([
  109.                 "minLength" => $minLength,
  110.                 "minStrength" => $minStrength,
  111.                 "tooShortMessage" => $this->translator->trans('validators.PasswordStrength.tooShort', ["%length%" => $minLength], "validators"),
  112.                 "message" => $this->translator->trans('validators.PasswordStrength.message', [], "validators"),
  113.             ])
  114.         ];
  115.         //Require non empty passwords
  116.         $passwordConstraints[] = new NotBlank();
  117.         if ($user && !$user->getUsername()) {
  118.             $form->add('plainPassword'PasswordType::class, [
  119.                 'label' => false,
  120.                 'attr' => [
  121.                     'placeholder' => 'general.password',
  122.                     'class' => 'form-control required-placeholder'
  123.                 ],
  124.                 'mapped' => false,
  125.                 'required' => true,
  126.                 'constraints' => $passwordConstraints,
  127.             ]);
  128.         } else {
  129.             $form->add('plainPassword'HiddenType::class, [
  130.                 'mapped' => false,
  131.                 'required' => false,
  132.                 'data' => bin2hex(random_bytes(16))
  133.             ]);
  134.         }
  135.     }
  136.     public function onPreSubmit(FormEvent $event): void
  137.     {
  138.         $form $event->getForm();
  139.         $data $event->getData();
  140.         if (empty($data['username']) && !empty($data['email'])) {
  141.             $email $data['email'];
  142.             //Make empty emails transform to NULL 
  143.             if (trim($email) === '') {
  144.                 $email null;
  145.             }
  146.             if (!$email) {
  147.                 $this->logger->error("Failed to retrieve email information to setup user.username field value in " __METHOD__);
  148.             }
  149.             $data['username'] = $email;
  150.             $event->setData($data);
  151.         }
  152.     }
  153.     public function onPostSubmit(FormEvent $event): void
  154.     {
  155.         $form $event->getForm();
  156.     }
  157.     public function configureOptions(OptionsResolver $resolver): void
  158.     {
  159.         $resolver->setDefaults([
  160.             'data_class' => User::class,
  161.         ]);
  162.     }
  163. }