src/Form/RegistrationFormType.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\User;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\Validator\Constraints\IsTrue;
  7. use Symfony\Component\Validator\Constraints\Length;
  8. use Symfony\Component\Validator\Constraints\NotBlank;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  12. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  13. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  14. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. class RegistrationFormType extends AbstractType
  19. {
  20.     private $translator;
  21.     private $requestStack;
  22.     private $session;
  23.     public function __construct(TranslatorInterface $translator,RequestStack $requestStack)
  24.     {
  25.         $this->setTranslator($translator);
  26.         $this->requestStack $requestStack;
  27.     }
  28.     public function buildForm(FormBuilderInterface $builder, array $options): void
  29.     {
  30.         $this->session $this->getRequestStack()->getSession();
  31.         $locale $this->getSession()->get('_locale''en');
  32.         $emaillable$this->getTranslator()->trans('email'$locale);
  33.         $pseudolable$this->getTranslator()->trans('pseudo'$locale);
  34.         $agreelable$this->getTranslator()->trans('agreeterms'$locale);
  35.         $agreemsglable$this->getTranslator()->trans('agreetermsmsg'$locale);
  36.         $acceptCgvlable$this->getTranslator()->trans('acceptCgv'$locale);
  37.         $acceptpromolable$this->getTranslator()->trans('acceptPubPromo'$locale);
  38.         $passlabel$this->getTranslator()->trans('password'$locale);
  39.         $passreplabel$this->getTranslator()->trans('passwordrep'$locale);
  40.         $builder
  41.             ->add('email',EmailType::class, ['label' => $emaillable])
  42.             ->add('pseudo',TextType::class, ['label' => $pseudolable])
  43.             ->add('agreeTerms'CheckboxType::class, [
  44.                 'label' => $agreelable,
  45.                 'mapped' => false,
  46.                 'constraints' => [
  47.                     new IsTrue([
  48.                         'message' => $agreemsglable,
  49.                     ]),
  50.                 ],
  51.             ])
  52.             ->add('acceptCgv'CheckboxType::class, [
  53.                 'label' => $acceptCgvlable,
  54.                 'mapped' => true,
  55.                 'constraints' => [
  56.                     new IsTrue([
  57.                         'message' => 'You should agree to our terms.',
  58.                     ]),
  59.                 ],
  60.             ])
  61.             ->add('acceptPublicationPromotion'CheckboxType::class, [
  62.                 'label' => $acceptpromolable,
  63.                 'mapped' => true,
  64.                 'required' => false
  65.             ])
  66.             ->add('plainPassword'RepeatedType::class, [
  67.                 'label' => $passlabel,
  68.                 'mapped' => false,
  69.                 'type' => PasswordType::class,
  70.                 'invalid_message' => 'The password fields must match.',
  71.                 'options' => ['attr' => ['class' => 'password-field']],
  72.                 'required' => true,
  73.                 'first_options'  => ['label' => $passlabel],
  74.                 'second_options' => ['label' => $passreplabel],
  75.             ]);
  76.         ;
  77.     }
  78.     public function configureOptions(OptionsResolver $resolver): void
  79.     {
  80.         $resolver->setDefaults([
  81.             'data_class' => User::class,
  82.         ]);
  83.     }
  84.     /**
  85.      * Get the value of translator
  86.      */ 
  87.     public function getTranslator()
  88.     {
  89.         return $this->translator;
  90.     }
  91.     /**
  92.      * Set the value of translator
  93.      *
  94.      * @return  self
  95.      */ 
  96.     public function setTranslator($translator)
  97.     {
  98.         $this->translator $translator;
  99.         return $this;
  100.     }
  101.     /**
  102.      * Get the value of requestStack
  103.      */ 
  104.     public function getRequestStack()
  105.     {
  106.         return $this->requestStack;
  107.     }
  108.     /**
  109.      * Get the value of session
  110.      */ 
  111.     public function getSession()
  112.     {
  113.         return $this->session;
  114.     }
  115. }