src/Admin/Modules/User/Form/ResetPasswordType.php line 15

  1. <?php
  2. namespace App\Admin\Modules\User\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. use Symfony\Component\Form\FormError;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Modules\User\Entity\User;
  12. class ResetPasswordType extends AbstractType
  13. {
  14.     public $user;
  15.     private $_doctrine;
  16.     public function __construct(ManagerRegistry $doctrine)
  17.     {
  18.         $this->_doctrine $doctrine;
  19.     }
  20.     public function buildForm(FormBuilderInterface $builder, array $options)
  21.     {
  22.         $builder
  23.             ->add('username'TextType::class, array(
  24.                 'label' => false,
  25.                 'attr' => array('maxlength' => 255'placeholder' => 'Nazwa użytkownika'),
  26.                 'constraints' => array(
  27.                     new Assert\NotBlank(),
  28.                     new Assert\Length(array('max' => 255))
  29.                 )
  30.             ))
  31.             ->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event)
  32.             {
  33.                 $form $event->getForm();
  34.                 
  35.                 if ($form['username']->getData() != '')
  36.                 {
  37.                     $this->user $this->_doctrine->getRepository(User::class)->findOneBy(['username' => $form['username']->getData(), 'type' => [User::TYPE_SUPER_ADMINUser::TYPE_ADMIN]]);
  38.                     if (!$this->user)
  39.                         $form['username']->addError(new FormError('Użytkownik nie istnieje.'));
  40.                 }
  41.             });
  42.     }
  43. }