src/Form/RegisterFormType.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Account;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  9. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\IsTrue;
  13. use Symfony\Component\Validator\Constraints\Length;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. class RegisterFormType extends AbstractType
  17. {
  18.     public function buildForm(FormBuilderInterface $builder, array $options)
  19.     {
  20.         $builder
  21.             ->add('name'TextType::class, [
  22.                 'attr' => ['class' => 'form-control'],
  23.             ])
  24.             ->add('organization'TextType::class, [
  25.                 'attr' => ['class' => 'form-control'],
  26.             ])
  27.             ->add('position'TextType::class, [
  28.                 'attr' => ['class' => 'form-control'],
  29.             ])
  30.             ->add('email'EmailType::class, [
  31.                 'attr' => ['class' => 'form-control'],
  32.                 'constraints' => [
  33.                     new Assert\NotBlank([
  34.                         'message' => 'El email es obligatorio',
  35.                     ]),
  36.                     new Assert\Email([
  37.                         'message' => 'El email no es vĂ¡lido',
  38.                     ]),
  39.                 ],
  40.             ]);
  41.         
  42.         if ($options['type'] == "register")
  43.         {
  44.             $builder
  45.                 ->add('plainPassword'PasswordType::class, [
  46.                     // instead of being set onto the object directly,
  47.                     // this is read and encoded in the controller
  48.                     'mapped' => false,
  49.                     'constraints' => [
  50.                         new NotBlank([
  51.                             'message' => 'Please enter a password',
  52.                         ]),
  53.                         new Length([
  54.                             'min' => 6,
  55.                             'minMessage' => 'Your password should be at least {{ limit }} characters',
  56.                             // max length allowed by Symfony for security reasons
  57.                             'max' => 4096,
  58.                         ]),
  59.                     ],
  60.                     'attr' => ['class' => 'form-control'],
  61.                 ])
  62.             ;
  63.         }
  64.     }
  65.     private function createFormChangePassword($builder)
  66.     {
  67.         $builder
  68.             ->add('plainPassword'RepeatedType::class, array(
  69.                 'type' => PasswordType::class,
  70.                 'constraints' => [
  71.                     new NotBlank([
  72.                         'message' => 'Please enter a password',
  73.                     ]),
  74.                     new Length([
  75.                         'min' => 6,
  76.                         'minMessage' => 'Your password should be at least 8 characters',
  77.                         // max length allowed by Symfony for security reasons
  78.                         'max' => 4096,
  79.                     ]),
  80.                 ],
  81.                 'first_options'  => array('label' => 'New Password'),
  82.                 'second_options' => array('label' => 'Repeat New Password'),
  83.             ))
  84.         ;
  85.     }
  86.     public function configureOptions(OptionsResolver $resolver)
  87.     {
  88.         $resolver->setDefaults([
  89.             'data_class' => Account::class,
  90.         ]);
  91.         $resolver->setRequired('type');
  92.     }
  93. }