src/Form/EventListener/AddObsadaFieldSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Form\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Form\Extension\Core\Type\TextType;
  5. use Symfony\Component\Form\FormEvents;
  6. use Symfony\Component\Form\FormFactoryInterface;
  7. use Symfony\Contracts\EventDispatcher\Event;
  8. class AddObsadaFieldSubscriber implements EventSubscriberInterface
  9. {
  10.     private $factory;
  11.     public function __construct(FormFactoryInterface $factory)
  12.     {
  13.         $this->factory $factory;
  14.     }
  15.     public static function getSubscribedEvents()
  16.     {
  17.         // Tells the dispatcher that you want to listen on the form.pre_set_data
  18.         // event and that the preSetData method should be called.
  19.         return array(FormEvents::POST_SUBMIT => 'preSetData');
  20.     }
  21.     public function preSetData(Event $event)
  22.     {
  23.         $data $event->getData();
  24.         $form $event->getForm();
  25.         // During form creation setData() is called with null as an argument
  26.         // by the FormBuilder constructor. You're only concerned with when
  27.         // setData is called with an actual Entity object in it (whether new
  28.         // or fetched with Doctrine). This if statement lets you skip right
  29.         // over the null condition.
  30.         if (null === $data) {
  31.             return;
  32.         }
  33.         // check if the product object is "new"
  34.        // if (!$data->getId()) {
  35.             $form->add('name4'TextType::class);
  36.        // }
  37.     }
  38. }