src/Form/RegistrationFormType.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Newsletter;
  4. use App\Entity\User;
  5. use Rollerworks\Component\PasswordStrength\Validator\Constraints\PasswordStrength;
  6. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  11. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Validator\Constraints\Length;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. class RegistrationFormType extends AbstractType
  18. {
  19. public function buildForm(FormBuilderInterface $builder, array $options)
  20. {
  21. $builder
  22. ->add('email', RepeatedType::class, [
  23. 'type' => emailType::class,
  24. 'attr' => ['class' => 'form-group'],
  25. 'invalid_message' => 'The email fields must match.',
  26. 'first_options' => ['label' => 'Email'],
  27. 'second_options' => ['label' => 'Repeat Email'],
  28. ])
  29. ->add('plainPassword', RepeatedType::class, [
  30. 'type' => PasswordType::class,
  31. 'attr' => ['class' => 'form-group'],
  32. 'invalid_message' => 'The password fields must match.',
  33. 'required' => true,
  34. 'mapped' => false,
  35. 'first_options' => ['label' => 'Password'],
  36. 'second_options' => ['label' => 'Repeat Password'],
  37. 'constraints' => [
  38. new NotBlank([
  39. 'message' => 'Please enter a password',
  40. ]),
  41. new PasswordStrength([
  42. 'minLength' => 8,
  43. 'minStrength' => 3,
  44. 'message' => 'Your password must be at least 8 characters and include both upper and lowercase letters, and at least one number or symbol.'
  45. ]),
  46. ],
  47. ])
  48. ->add('forename', TextType::class, [
  49. 'label' => 'Forename(s)',
  50. ])
  51. ->add('surname', TextType::class, [
  52. 'label' => 'Surname',
  53. ])
  54. ->add('title', ChoiceType::class, [
  55. 'label' => 'Title',
  56. 'placeholder' => '- Please Select -',
  57. 'choices' => array_flip(User::TITLES),
  58. ])
  59. ->add('subscriptions', EntityType::class, [
  60. 'class' => Newsletter::class,
  61. 'label' => 'Email me updates on',
  62. 'choice_label' => 'title',
  63. 'expanded' => true,
  64. 'multiple' => true,
  65. ]);
  66. }
  67. public function configureOptions(OptionsResolver $resolver)
  68. {
  69. $resolver->setDefaults([
  70. 'data_class' => User::class,
  71. ]);
  72. }
  73. }