src/UserPanel/Modules/User/Form/RegistrationType.php line 41

  1. <?php
  2. namespace App\UserPanel\Modules\User\Form;
  3. use Symfony\Component\OptionsResolver\OptionsResolver;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\Form\Form;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. use Symfony\Component\Form\FormError;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use App\Admin\Modules\Contract\Entity\ContractClient;
  18. use App\Modules\User\Service\UserService;
  19. use App\Modules\User\Entity\User;
  20. class RegistrationType extends AbstractType
  21. {
  22.     private $_router;
  23.     private $_doctrine;
  24.     private $_userService;
  25.     
  26.     protected $password;
  27.     public function __construct(
  28.             RouterInterface $router,
  29.             ManagerRegistry $doctrine,
  30.             UserService $userService)
  31.     {
  32.         $this->_router $router;
  33.         $this->_doctrine $doctrine;
  34.         $this->_userService $userService;
  35.     }
  36.     public function configureOptions(OptionsResolver $resolver)
  37.     {
  38.         $user = new User();
  39.         $user->setClient(new ContractClient());
  40.         $resolver->setDefaults(array(
  41.             'data_class' => User::class,
  42.             'data' => $user
  43.         ));
  44.     }
  45.     public function buildForm(FormBuilderInterface $builder, array $options)
  46.     {
  47.         $builder
  48.             ->setAction($this->_router->generate('user_panel_user_register'))
  49.             ->add('firstName'TextType::class, array(
  50.                 'label' => 'Imię',
  51.                 'property_path' => 'client.agentFirstName',
  52.                 'attr' => array('maxlength' => 255),
  53.                 'constraints' => array(
  54.                     new Assert\NotBlank(),
  55.                     new Assert\Length(['max' => 255])
  56.                 )
  57.             ))
  58.             ->add('lastName'TextType::class, array(
  59.                 'label' => 'Nazwisko',
  60.                 'property_path' => 'client.agentLastName',
  61.                 'attr' => array('maxlength' => 255),
  62.                 'constraints' => array(
  63.                     new Assert\NotBlank(),
  64.                     new Assert\Length(['max' => 255])
  65.                 )
  66.             ))
  67.             ->add('email'EmailType::class, array(
  68.                 'label' => 'E-mail',
  69.                 'property_path' => 'client.email',
  70.                 'attr' => array('maxlength' => 255),
  71.                 'constraints' => array(
  72.                     new Assert\NotBlank(),
  73.                     new Assert\Length(['max' => 255])
  74.                 )
  75.             ))
  76.             ->add('password'RepeatedType::class, array(
  77.                 'type' => PasswordType::class,
  78.                 'mapped' => false,
  79.                 'first_options'  => array(
  80.                     'label' => 'Hasło',
  81.                     'always_empty' => false,
  82.                     'attr' => ['value' => $this->password],
  83.                     'constraints' => array(
  84.                         new Assert\NotBlank(),
  85.                         new Assert\Length(['min' => 8])
  86.                     )
  87.                 ),
  88.                 'second_options' => array(
  89.                     'label' => 'Powtórz hasło',
  90.                     'always_empty' => false,
  91.                     'attr' => ['value' => $this->password],
  92.                     'constraints' => array(
  93.                         new Assert\NotBlank(),
  94.                         new Assert\Length(['min' => 8])
  95.                     )
  96.                 ),
  97.                 'invalid_message' => 'Podane hasła są różne.'
  98.             ))
  99.             ->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($options)
  100.             {
  101.                 $form $event->getForm();
  102.                 $data $event->getData();
  103.                 $this->_validateForm($form$data$options);
  104.             });
  105.     }
  106.     
  107.     public function _validateForm(Form $formUser $data$formOptions)
  108.     {
  109.         $repository $this->_doctrine->getRepository(User::class);
  110.         if ($form['email']->getData() != '')
  111.         {
  112.             // nazwa uzytkownika
  113.             $user $repository->findOneByUsername($form['email']->getData());
  114.             if ($user)
  115.                 $form['email']->addError(new FormError('Adres email już istnieje.'));
  116.             else
  117.             {
  118.                 // email
  119.                 $user $repository->findOneByEmail($form['email']->getData());
  120.                 if ($user)
  121.                     $form['email']->addError(new FormError('Adres email już istnieje.'));
  122.             }
  123.         }
  124.     }
  125. }