src/Form/RegistrationFormType.php line 17

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\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\IsTrue;
  12. use Symfony\Component\Validator\Constraints\Length;
  13. use Symfony\Component\Validator\Constraints\NotBlank;
  14. class RegistrationFormType extends AbstractType
  15. {
  16.     public function buildForm(FormBuilderInterface $builder, array $options): void
  17.     {
  18.         $builder
  19.             ->add('username')
  20.             ->add('plainPassword'RepeatedType::class, [
  21.                 // instead of being set onto the object directly,
  22.                 // this is read and encoded in the controller
  23.                 'type' => PasswordType::class,
  24.                 'mapped' => false,
  25.                 'attr' => ['autocomplete' => 'new-password'],
  26.                 'first_options'  => ['label' => 'Password'],
  27.                 'second_options' => ['label' => 'Repeat Password'],
  28.                 'constraints' => [
  29.                     new NotBlank([
  30.                         'message' => 'Please enter a password',
  31.                     ]),
  32.                     new Length([
  33.                         'min' => 6,
  34.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  35.                         // max length allowed by Symfony for security reasons
  36.                         'max' => 4096,
  37.                     ]),
  38.                 ],
  39.             ])
  40.         ;
  41.     }
  42.     public function configureOptions(OptionsResolver $resolver): void
  43.     {
  44.         $resolver->setDefaults([
  45.             'data_class' => User::class,
  46.         ]);
  47.     }
  48. }