src/Form/RegistrationFormType.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\AppUser;
  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\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', EmailType::class)
  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', PasswordType::class, [
  28. // instead of being set onto the object directly,
  29. // this is read and encoded in the controller
  30. 'mapped' => false,
  31. 'label' => 'Password',
  32. 'toggle' => true,
  33. 'button_classes' => ['input-group-text', 'bg-white'],
  34. 'toggle_container_classes' => ['input-group'],
  35. 'visible_label' => null,
  36. 'hidden_label' => null,
  37. 'attr' => ['autocomplete' => 'new-password'],
  38. 'constraints' => [
  39. new NotBlank([
  40. 'message' => 'Please enter a password',
  41. ]),
  42. new Length([
  43. 'min' => 6,
  44. 'minMessage' => 'Your password should be at least {{ limit }} characters',
  45. // max length allowed by Symfony for security reasons
  46. 'max' => 4096,
  47. ]),
  48. ],
  49. ])
  50. ;
  51. }
  52. public function configureOptions(OptionsResolver $resolver): void
  53. {
  54. $resolver->setDefaults([
  55. 'data_class' => AppUser::class,
  56. ]);
  57. }
  58. }