Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony adding EventListener in a closure and getting NULL
    primarykey
    data
    text
    <p>I'm getting error "Call to a member function getAgent() on a non-object".</p> <p>Here is my code in AgentsType.php:</p> <pre><code>public function buildForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('agents', 'entity', array( 'class' =&gt; 'MyBundle:agents', 'property' =&gt; 'name',)); $formModifier = function(FormInterface $form, agents $agent) { $description = $agent-&gt;getDescription(); $form-&gt;add('description', 'text'); }; $builder-&gt;addEventListener( FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($formModifier) { $data = $event-&gt;getData(); //ERROR HERE, returns NULL $formModifier($event-&gt;getForm(), $data-&gt;getAgent()); // Exception HERE } ); $builder-&gt;get('agents')-&gt;addEventListener( FormEvents::POST_SUBMIT, function(FormEvent $event) use ($formModifier) { $agent = $event-&gt;getForm()-&gt;getData(); $formModifier($event-&gt;getForm()-&gt;getParent(), $agent); } ); } </code></pre> <p>$event->getData(); is NULL, I var_dumped $event and it's a very big amount of objects.</p> <p>The purpose is to generate the form dynamically after choosing an agent entity in a select field, in this case a description field.</p> <p>Can anyone advice me what to do with this error or why is getData() = NULL??</p> <p>EDIT:</p> <p>The main purpose of this is that the user can add/disable/update Agents. The main idea is to show just a select or dropdown field and an "Add Agent" button. If the user selects one name from the select field, then is the form for the Agents Entity filled with its data. That's the reason to have an EventListener, to detect any selection in the select field, and then show the data according to the user selection. Hope this clarifies a bit the goal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload