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\CheckboxType;
  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\IsTrue;
  10. use Symfony\Component\Validator\Constraints\Length;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. class RegistrationFormType extends AbstractType
  13. {
  14.     public function buildForm(FormBuilderInterface $builder, array $options): void
  15.     {
  16.         $builder
  17.             ->add('email')
  18.             ->add('agreeTerms'CheckboxType::class, [
  19.                 'mapped' => false,
  20.                 'constraints' => [
  21.                     new IsTrue([
  22.                         'message' => 'You should agree to our terms.',
  23.                     ]),
  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.                 'mapped' => false,
  30.                 'attr' => ['autocomplete' => 'new-password'],
  31.                 'constraints' => [
  32.                     new NotBlank([
  33.                         'message' => 'Please enter a password',
  34.                     ]),
  35.                     new Length([
  36.                         'min' => 6,
  37.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  38.                         // max length allowed by Symfony for security reasons
  39.                         'max' => 4096,
  40.                     ]),
  41.                 ],
  42.             ])
  43.             ->add('confirmPassword'PasswordType::class, [
  44.                 // instead of being set onto the object directly,
  45.                 // this is read and encoded in the controller
  46.                 'mapped' => false,
  47.                 'attr' => ['autocomplete' => 'new-password'],
  48.                 'constraints' => [
  49.                     new NotBlank([
  50.                         'message' => 'Please enter a password',
  51.                     ]),
  52.                     new Length([
  53.                         'min' => 6,
  54.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  55.                         // max length allowed by Symfony for security reasons
  56.                         'max' => 4096,
  57.                     ]),
  58.                 ],
  59.             ])
  60.         ;
  61.     }
  62.     public function configureOptions(OptionsResolver $resolver): void
  63.     {
  64.         $resolver->setDefaults([
  65.             'data_class' => User::class,
  66.         ]);
  67.     }
  68. }