src/Form/RegistrationFormType.php line 15

  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('fname')
  18.             ->add('lname')
  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'PasswordType::class, [
  29.                 // instead of being set onto the object directly,
  30.                 // this is read and encoded in the controller
  31.                 'mapped' => false,
  32.                 'attr' => ['autocomplete' => 'new-password'],
  33.                 'constraints' => [
  34.                     new NotBlank([
  35.                         'message' => 'Please enter a password',
  36.                     ]),
  37.                     new Length([
  38.                         'min' => 6,
  39.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  40.                         // max length allowed by Symfony for security reasons
  41.                         'max' => 4096,
  42.                     ]),
  43.                 ],
  44.             ])
  45.             ->add('plainPasswordRepeat'PasswordType::class, [
  46.                 // instead of being set onto the object directly,
  47.                 // this is read and encoded in the controller
  48.                 'mapped' => false,
  49.                 'attr' => ['autocomplete' => 'retype-password'],
  50.                 'constraints' => [
  51.                     new NotBlank([
  52.                         'message' => 'Please enter a password',
  53.                     ]),
  54.                     new Length([
  55.                         'min' => 6,
  56.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  57.                         // max length allowed by Symfony for security reasons
  58.                         'max' => 4096,
  59.                     ]),
  60.                 ],
  61.             ])
  62.         ;
  63.     }
  64.     public function configureOptions(OptionsResolver $resolver): void
  65.     {
  66.         $resolver->setDefaults([
  67.             'data_class' => User::class,
  68.         ]);
  69.     }
  70. }