src/UserPanel/Modules/User/Form/RegistrationType.php line 41
<?phpnamespace App\UserPanel\Modules\User\Form;use Symfony\Component\OptionsResolver\OptionsResolver;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\Form\Extension\Core\Type\TextType;use Symfony\Component\Form\Extension\Core\Type\EmailType;use Symfony\Component\Form\Extension\Core\Type\RepeatedType;use Symfony\Component\Form\Extension\Core\Type\PasswordType;use Symfony\Component\Form\Form;use Symfony\Component\Form\FormEvent;use Symfony\Component\Form\FormEvents;use Symfony\Component\Form\FormError;use Symfony\Component\Validator\Constraints as Assert;use Symfony\Component\Routing\RouterInterface;use Doctrine\Persistence\ManagerRegistry;use App\Admin\Modules\Contract\Entity\ContractClient;use App\Modules\User\Service\UserService;use App\Modules\User\Entity\User;class RegistrationType extends AbstractType{private $_router;private $_doctrine;private $_userService;protected $password;public function __construct(RouterInterface $router,ManagerRegistry $doctrine,UserService $userService){$this->_router = $router;$this->_doctrine = $doctrine;$this->_userService = $userService;}public function configureOptions(OptionsResolver $resolver){$user = new User();$user->setClient(new ContractClient());$resolver->setDefaults(array('data_class' => User::class,'data' => $user));}public function buildForm(FormBuilderInterface $builder, array $options){$builder->setAction($this->_router->generate('user_panel_user_register'))->add('firstName', TextType::class, array('label' => 'Imię','property_path' => 'client.agentFirstName','attr' => array('maxlength' => 255),'constraints' => array(new Assert\NotBlank(),new Assert\Length(['max' => 255]))))->add('lastName', TextType::class, array('label' => 'Nazwisko','property_path' => 'client.agentLastName','attr' => array('maxlength' => 255),'constraints' => array(new Assert\NotBlank(),new Assert\Length(['max' => 255]))))->add('email', EmailType::class, array('label' => 'E-mail','property_path' => 'client.email','attr' => array('maxlength' => 255),'constraints' => array(new Assert\NotBlank(),new Assert\Length(['max' => 255]))))->add('password', RepeatedType::class, array('type' => PasswordType::class,'mapped' => false,'first_options' => array('label' => 'Hasło','always_empty' => false,'attr' => ['value' => $this->password],'constraints' => array(new Assert\NotBlank(),new Assert\Length(['min' => 8]))),'second_options' => array('label' => 'Powtórz hasło','always_empty' => false,'attr' => ['value' => $this->password],'constraints' => array(new Assert\NotBlank(),new Assert\Length(['min' => 8]))),'invalid_message' => 'Podane hasła są różne.'))->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($options){$form = $event->getForm();$data = $event->getData();$this->_validateForm($form, $data, $options);});}public function _validateForm(Form $form, User $data, $formOptions){$repository = $this->_doctrine->getRepository(User::class);if ($form['email']->getData() != ''){// nazwa uzytkownika$user = $repository->findOneByUsername($form['email']->getData());if ($user)$form['email']->addError(new FormError('Adres email już istnieje.'));else{// email$user = $repository->findOneByEmail($form['email']->getData());if ($user)$form['email']->addError(new FormError('Adres email już istnieje.'));}}}}