src/Form/RegistrationFormType.php line 20

  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\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  8. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Component\Validator\Constraints\All;
  12. use Symfony\Component\Validator\Constraints\IsTrue;
  13. use Symfony\Component\Validator\Constraints\Length;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. use Symfony\Component\Validator\Constraints\Email;
  16. class RegistrationFormType extends AbstractType
  17. {
  18.     public function buildForm(FormBuilderInterface $builder, array $options): void
  19.     {
  20.         $builder
  21.             ->add('lastName')
  22.             ->add('firstName')
  23.             ->add('email',EmailType::class,[
  24.                 'constraints' => [
  25.                     new Email([
  26.                         'message' => 'Veuillez entrer une adresse email valide'
  27.                     ]),
  28.                     new NotBlank([
  29.                         'message' => 'Veuillez entrer une adresse email valide',
  30.                     ]),
  31.                 ]
  32.             ])
  33.             ->add('plainPassword'RepeatedType::class, [
  34.                 // instead of being set onto the object directly,
  35.                 // this is read and encoded in the controller
  36.                 'type' => PasswordType::class,
  37.                 'mapped' => false,
  38.                 'attr' => ['autocomplete' => 'new-password'],
  39.                 'invalid_message' => 'Les mots de passe ne correspondent pas',
  40.                 'first_options' => [
  41.                     'constraints' => [
  42.                         new NotBlank([
  43.                             'message' => 'Please enter a password',
  44.                         ]),
  45.                         new Length([
  46.                             'min' => 6,
  47.                             'minMessage' => 'Your password should be at least {{ limit }} characters',
  48.                             // max length allowed by Symfony for security reasons
  49.                             'max' => 4096,
  50.                         ]),
  51.                     ],
  52.                     'label' => 'Mot de passe',
  53.                 ],
  54.                 'second_options' => [
  55.                     'label' => 'Répéter mot de passe',
  56.                 ],
  57.             ])
  58.             ->add('agreeTerms'CheckboxType::class, [
  59.                 'mapped' => false,
  60.                 'constraints' => [
  61.                     new IsTrue([
  62.                         'message' => 'You should agree to our terms.',
  63.                     ]),
  64.                 ],
  65.             ])
  66.         ;
  67.     }
  68.     public function configureOptions(OptionsResolver $resolver): void
  69.     {
  70.         $resolver->setDefaults([
  71.             'data_class' => User::class,
  72.         ]);
  73.     }
  74. }