src/Form/RegistrationFormType.php line 16

  1. <?php
  2. namespace Cms\Form;
  3. use Cms\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\FormBuilderInterface;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Component\Validator\Constraints\IsTrue;
  11. use Symfony\Component\Validator\Constraints\Length;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. class RegistrationFormType extends AbstractType
  14. {
  15.     public function buildForm(FormBuilderInterface $builder, array $options): void
  16.     {
  17.         $builder
  18.             ->add('email')
  19.             ->add('agreeTerms'CheckboxType::class, [
  20.                 'mapped' => false,
  21.                 'constraints' => [
  22.                     new IsTrue([
  23.                         'message' => 'You should agree to our terms.',
  24.                     ]),
  25.                 ],
  26.             ])
  27.             ->add('plainPassword'RepeatedType::class, [
  28.                 'type' => 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.         ;
  46.     }
  47.     public function configureOptions(OptionsResolver $resolver): void
  48.     {
  49.         $resolver->setDefaults([
  50.             'data_class' => User::class,
  51.         ]);
  52.     }
  53. }