src/Form/RegistrationFormType.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\User;
  4. use App\Validator\AuditAnalyticsEmail;
  5. use Psr\Container\ContainerInterface;
  6. use Symfony\Component\Form\AbstractType;
  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\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\Length;
  13. use Symfony\Component\Validator\Constraints\NotBlank;
  14. class RegistrationFormType extends AbstractType
  15. {
  16.     private array $allowedEmails;
  17.     /**
  18.      * RegistrationFormType constructor.
  19.      * @param $allowedEmails
  20.      */
  21.     public function __construct(array $allowedEmails)
  22.     {
  23.         $this->allowedEmails $allowedEmails;
  24.     }
  25.     public function buildForm(FormBuilderInterface $builder, array $options)
  26.     {
  27.         $builder
  28.             ->add('email'EmailType::class, [
  29.                 'required' => true,
  30.                 'constraints' => [
  31.                     new AuditAnalyticsEmail([
  32.                         'allowedEmails' => $this->allowedEmails
  33.                     ])
  34.                 ]
  35.             ])
  36.             ->add('firstName')
  37.             ->add('lastName')
  38.             ->add('plainPassword'RepeatedType::class, [
  39.                 // instead of being set onto the object directly,
  40.                 // this is read and encoded in the controller
  41.                 'type' => PasswordType::class,
  42.                 'mapped' => false,
  43.                 'constraints' => [
  44.                     new NotBlank([
  45.                         'message' => 'Please enter a password',
  46.                     ]),
  47.                     new Length([
  48.                         'min' => 6,
  49.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  50.                         // max length allowed by Symfony for security reasons
  51.                         'max' => 4096,
  52.                     ]),
  53.                 ],
  54.                 'first_options' => ['label' => 'Password'],
  55.                 'second_options' => ['label' => 'Confirm Password'],
  56.                 'invalid_message' => 'Your password does not match the confirmation.',
  57.             ])
  58.         ;
  59.     }
  60.     public function configureOptions(OptionsResolver $resolver)
  61.     {
  62.         $resolver->setDefaults([
  63.             'data_class' => User::class,
  64.         ]);
  65.     }
  66. }