src/Form/RegistrationFormType.php line 20

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\Extension\Core\Type\CheckboxType;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  9. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  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\Validator\Constraints\IsTrue;
  14. use Symfony\Component\Validator\Constraints\Length;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. use Symfony\Component\Form\CallbackTransformer;
  17. class RegistrationFormType extends AbstractType
  18. {
  19.     public function buildForm(FormBuilderInterface $builder, array $options): void
  20.     {
  21.         $builder
  22.             ->add('email'EmailType::class, [
  23.                 'label'      => 'Adresse email',
  24.                 'row_attr'   => ['class' => 'form-floating mb-2'],
  25.                 'attr'       => ['class' => 'form-control'],
  26.                 'label_attr' => ['class' => 'form-label'],
  27.             ])
  28.             ->add('firstName'TextType::class, [
  29.                 'label'      => 'PrĂ©nom',
  30.                 'row_attr'   => ['class' => 'form-floating mb-2'],
  31.                 'attr'       => ['class' => 'form-control'],
  32.                 'label_attr' => ['class' => 'form-label'],
  33.             ])
  34.             ->add('lastName'TextType::class, [
  35.                 'label'      => 'Nom',
  36.                 'row_attr'   => ['class' => 'form-floating mb-2'],
  37.                 'attr'       => ['class' => 'form-control'],
  38.                 'label_attr' => ['class' => 'form-label'],
  39.             ])
  40.             ->add('plainPassword'RepeatedType::class, [
  41.                 'type' => PasswordType::class,
  42.                 'first_options'  => [
  43.                     'label' => 'Mot de passe',
  44.                     'row_attr'    => ['class' => 'form-floating mb-2'],
  45.                     'attr'        => ['class' => 'form-control'],
  46.                     'label_attr'  => ['class' => 'form-label'],
  47.                 ],
  48.                 'second_options' => [
  49.                     'label' => 'Repeter le mot de passe',
  50.                     'row_attr'    => ['class' => 'form-floating mb-2'],
  51.                     'attr'        => ['class' => 'form-control'],
  52.                     'label_attr'  => ['class' => 'form-label'],
  53.                 ],
  54.                 'invalid_message' => 'The password fields must match.',
  55.                 'required' => true,
  56.                 'mapped'      => false,
  57.                 'constraints' => [
  58.                     new NotBlank([
  59.                         'message' => 'Please enter a password',
  60.                     ]),
  61.                     new Length([
  62.                         'min'        => 6,
  63.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  64.                         'max'        => 4096,
  65.                         // max length allowed by Symfony for security reasons
  66.                     ]),
  67.                 ],
  68.             ])
  69.             ->add('roles'ChoiceType::class, [
  70.                 'label'      => 'Vous etes ...',
  71.                 'choices' => [
  72.                     'parent' => 'ROLE_PARENT_REGISTERED',
  73.                     'ecole' => 'ROLE_SCHOOL_REGISTERED',
  74.                 ],
  75.                 'required' => true,
  76.                 'multiple' => false,
  77.                 'expanded' => false,
  78.                 'row_attr'   => ['class' => 'form-floating mb-2'],
  79.                 'attr'       => ['class' => 'form-control'],
  80.                 'label_attr' => ['class' => 'form-label'],
  81.             ])
  82.             ->add('agreeTerms'CheckboxType::class, [
  83.                 'label'       => 'Accepter les conditions',
  84.                 'row_attr'    => ['class' => 'd-flex mb-2'],
  85.                 'attr'        => ['class' => 'me-2'],
  86.                 'label_attr'  => ['class' => 'form-check-label'],
  87.                 'mapped'      => false,
  88.                 'constraints' => [
  89.                     new IsTrue([
  90.                         'message' => 'You should agree to our terms.',
  91.                     ]),
  92.                 ],
  93.             ]);
  94.         // Data transformer
  95.         $builder->get('roles')
  96.             ->addModelTransformer(new CallbackTransformer(
  97.                 function ($rolesArray) {
  98.                      // transform the array to a string
  99.                      return count($rolesArray)? $rolesArray[0]: null;
  100.                 },
  101.                 function ($rolesString) {
  102.                      // transform the string back to an array
  103.                      return [$rolesString];
  104.                 }
  105.         ));
  106.     }
  107.     public function configureOptions(OptionsResolver $resolver): void
  108.     {
  109.         $resolver->setDefaults([
  110.             'data_class' => User::class,
  111.         ]);
  112.     }
  113. }