src/Form/RegistrationFormType.php line 15

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\EmailType;
  6. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Component\Validator\Constraints\Length;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class RegistrationFormType extends AbstractType
  13. {
  14.     public function __construct(private TranslatorInterface $translator)
  15.     {
  16.     }
  17.     public function buildForm(FormBuilderInterface $builder, array $options): void
  18.     {
  19.         $builder
  20.             ->add('email'EmailType::class, [
  21.                 'label' => $this->translator->trans('form.personal.email', [], 'form_labels'),
  22.                 'attr' => [
  23.                     'class' => "form-control"
  24.                 ]
  25.             ])
  26.             ->add('plainPassword'PasswordType::class, [
  27.                 // instead of being set onto the object directly,
  28.                 // this is read and encoded in the controller
  29.                 'label' => $this->translator->trans('form.personal.password', [], 'form_labels'),
  30.                 'mapped' => false,
  31.                 'attr' => [
  32.                     'autocomplete' => 'new-password',
  33.                     'class' => "form-control"
  34.                     ],
  35.                 'constraints' => [
  36.                     new NotBlank([
  37.                         'message' => 'Please enter a password',
  38.                     ]),
  39.                     new Length([
  40.                         'min' => 6,
  41.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  42.                         // max length allowed by Symfony for security reasons
  43.                         'max' => 4096,
  44.                     ]),
  45.                 ],
  46.             ])
  47.         ;
  48.     }
  49.     public function configureOptions(OptionsResolver $resolver): void
  50.     {
  51.         $resolver->setDefaults([
  52.             'data_class' => User::class,
  53.         ]);
  54.     }
  55. }