src/Form/RegistrationFormType.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Gender;
  4. use App\Entity\User;
  5. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Component\Translation\TranslatableMessage;
  14. use Symfony\Component\Validator\Constraints\IsTrue;
  15. use Symfony\Component\Validator\Constraints\Length;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class RegistrationFormType extends AbstractType
  19. {
  20.     private TranslatorInterface $translator;
  21.     public function __construct(TranslatorInterface $translator){
  22.         $this->translator $translator;
  23.     }
  24.     public function buildForm(FormBuilderInterface $builder, array $options): void
  25.     {
  26.         $builder
  27.             ->add('email'EmailType::class, [
  28.                 'attr' => [
  29.                     'class' => 'form-control'
  30.                 ]
  31.             ])
  32.             ->add('username'TextType::class, [
  33.                 'attr' => [
  34.                     'class' => 'form-control'
  35.                 ]
  36.             ])
  37.             ->add('firstName'TextType::class, [
  38.                 'required' => false,
  39.                 'attr' => [
  40.                     'class' => 'form-control'
  41.                 ]
  42.             ])
  43.             ->add('lastName'TextType::class, [
  44.                 'required' => false,
  45.                 'attr' => [
  46.                     'class' => 'form-control'
  47.                 ]
  48.             ])
  49.             ->add('mobile'TextType::class, [
  50.                 'required' => false,
  51.                 'attr' => [
  52.                     'class' => 'form-control'
  53.                 ]
  54.             ])
  55.             ->add('agreeTerms'CheckboxType::class, [
  56.                 'mapped' => false,
  57.                 'attr' => [
  58.                     'class' => 'form-check-input'
  59.                 ],
  60.                 'constraints' => [
  61.                     new IsTrue([
  62.                         'message' => 'You should agree to our terms.',
  63.                     ]),
  64.                 ],
  65.             ])
  66.             ->add('plainPassword'PasswordType::class, [
  67.                 // instead of being set onto the object directly,
  68.                 // this is read and encoded in the controller
  69.                 'mapped' => false,
  70.                 'attr' => ['autocomplete' => 'new-password''class' => 'form-control' ],
  71.                 'constraints' => [
  72.                     new NotBlank([
  73.                         'message' => 'Please enter a password',
  74.                     ]),
  75. //                    new Length([
  76. //                        'min' => 4,
  77. //                        'minMessage' => 'Your password should be at least {{ limit }} characters',
  78. //                        // max length allowed by Symfony for security reasons
  79. //                        'max' => 4096,
  80. //                    ]),
  81.                 ],
  82.             ])->add('gender'EntityType::class, [
  83.                 'class' => Gender::class,
  84.                 'choice_label' => function($choice){
  85.                     return $this->translator->trans($choice->getType());
  86.                 },
  87.                 'attr' => [
  88.                     'class' => 'form-select'
  89.                 ],
  90.             ])
  91.         ;
  92.     }
  93.     public function configureOptions(OptionsResolver $resolver): void
  94.     {
  95.         $resolver->setDefaults([
  96.             'data_class' => User::class,
  97.         ]);
  98.     }
  99. }