src/Form/RegistrationFormType.php line 15

Open in your IDE?
  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)
  15.     {
  16.         $builder
  17.             ->add('username')
  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'PasswordType::class, [
  28.                 // instead of being set onto the object directly,
  29.                 // this is read and encoded in the controller
  30.                 'mapped' => false,
  31.                 'attr' => ['autocomplete' => 'new-password'],
  32.                 'constraints' => [
  33.                     new NotBlank([
  34.                         'message' => 'Please enter a password',
  35.                     ]),
  36.                     new Length([
  37.                         'min' => 6,
  38.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  39.                         // max length allowed by Symfony for security reasons
  40.                         'max' => 4096,
  41.                     ]),
  42.                 ],
  43.             ])
  44.         ;
  45.     }
  46.     public function configureOptions(OptionsResolver $resolver)
  47.     {
  48.         $resolver->setDefaults([
  49.             'data_class' => User::class,
  50.         ]);
  51.     }
  52. }