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\PasswordType;
  7. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  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('email')
  20.             ->add('agreeTerms'CheckboxType::class, [
  21.                 'mapped' => false,
  22.                 'constraints' => [
  23.                     new IsTrue([
  24.                         'message' => 'You should agree to our terms.',
  25.                     ]),
  26.                 ],
  27.             ])
  28.             ->add('plainPassword'RepeatedType::class, [
  29.                 'type' => PasswordType::class,
  30.                 'first_options' => [
  31.                     'attr' => ['autocomplete' => 'new-password'],
  32.                     'constraints' => [
  33.                         new NotBlank([
  34.                             'message' => 'Please enter a password',
  35.                         ]),
  36.                         new Length([
  37.                             'min' => 6,
  38.                             'minMessage' => 'Your password should be at least {{ limit }} characters',
  39.                             // max length allowed by Symfony for security reasons
  40.                             'max' => 4096,
  41.                         ]),
  42.                     ],
  43.                     'label' => 'New password',
  44.                 ],
  45.                 'second_options' => [
  46.                     'attr' => ['autocomplete' => 'new-password'],
  47.                     'label' => 'Repeat Password',
  48.                 ],
  49.                 'invalid_message' => 'The password fields must match.',
  50.                 // Instead of being set onto the object directly,
  51.                 // this is read and encoded in the controller
  52.                 'mapped' => false,
  53.             ])
  54.             ->add('register'SubmitType::class)
  55.         ;
  56.     }
  57.     public function configureOptions(OptionsResolver $resolver): void
  58.     {
  59.         $resolver->setDefaults([
  60.             'data_class' => User::class,
  61.         ]);
  62.     }
  63. }