src/Form/RegistrationFormType.php line 19

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\CallbackTransformer;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\DateType;
  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\TelType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. use Symfony\Component\Validator\Constraints\Regex;
  16. class RegistrationFormType extends AbstractType
  17. {
  18.     public function buildForm(FormBuilderInterface $builder, array $options): void
  19.     {
  20.         $builder
  21.             ->add('email', EmailType::class, [
  22.                 'label' => 'E-mail'
  23.             ])
  24.             ->add('password', PasswordType::class, [
  25.                 // instead of being set onto the object directly,
  26.                 // this is read and encoded in the controller
  27.                 'mapped' => false,
  28.                 'attr' => ['autocomplete' => 'new-password'],
  29.                 'constraints' => [
  30.                     new NotBlank([
  31.                         'message' => 'Please enter a password',
  32.                     ]),
  33.                     new Regex([
  34.                         'pattern' =>  '/^(?=.*\d)(?=.*[A-Z])(?=.*[#?!@$%^&*-])(?!.*(.)\1{2}).*[a-z]/m',
  35.                         'match' => true,
  36.                         'message' => "Votre mot de passe doit comporter au moins six caractères, une majuscule, une minuscule, un chiffre et un caractère spécial.",
  37.                     ]),
  38.                 ],
  39.             ])
  40.             ->add('firstname_user', TextType::class)
  41.             ->add('lastname_user', TextType::class)
  42.             ->add('phone_user', TelType::class, [
  43.                 'constraints' => [
  44.                     new Regex([
  45.                         'pattern' =>  '/^(\+\d{1,3}\s)?\d{8,15}$/',
  46.                         'match' => true,
  47.                         'message' => "Votre numéro de téléphone doit comporter des nombres.",
  48.                     ]),
  49.                 ],
  50.             ])
  51.             ->add('address_user', TextType::class)
  52.             ->add('birthdate_user', DateType::class, array(
  53.                 'widget' => 'choice',
  54.                 'format' => 'ddMMyyyy',
  55.                 'years' => range(date('Y')-140, date('Y')),
  56.                 'data' => new \DateTime(),
  57.               ))
  58.             ->add('roles', ChoiceType::class, [
  59.                 'choices' => [
  60.                     'ADMIN' => 'ROLE_ADMIN',
  61.                     'USER' => 'ROLE_USER',
  62.                 ],
  63.                 'expanded' => true,
  64.                 'multiple' => false,
  65.             ]);
  66.         $builder->get('roles')
  67.             ->addModelTransformer(new CallbackTransformer(
  68.                 function ($rolesArray) {
  69.                     // transform the array to a string
  70.                     return count($rolesArray) ? $rolesArray[0] : null;
  71.                 },
  72.                 function ($rolesString) {
  73.                     // transform the string back to an array
  74.                     return [$rolesString];
  75.                 }
  76.             ));
  77.     }
  78.     public function configureOptions(OptionsResolver $resolver): void
  79.     {
  80.         $resolver->setDefaults([
  81.             'data_class' => User::class,
  82.         ]);
  83.     }
  84. }