src/Form/Subscriber/UserSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Form\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  5. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. class UserSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @inheritDoc
  12.      */
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [FormEvents::PRE_SET_DATA => 'onPreSetData'];
  16.     }
  17.     /**
  18.      * Adds password fields to form only when creating a new user
  19.      *
  20.      * @param FormEvent $event
  21.      */
  22.     public function onPreSetData(FormEvent $event)
  23.     {
  24.         $user $event->getData();
  25.         if (null === $user->getId()) {
  26.             $form $event->getForm();
  27.             $form->add('password'RepeatedType::class, [
  28.                 'type' => PasswordType::class,
  29.                 'first_options' => ['label' => 'Password'],
  30.                 'second_options' => ['label' => 'Repeat Password'],
  31.             ]);
  32.         }
  33.     }
  34. }