src/Form/RegistrationFormType.php line 17

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