Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd event listener to form element added by event listener
    primarykey
    data
    text
    <p>I'm building a Symfony app and using form events with some jquery/ajax to do the whole "state/locality" thing. I have a slight issue though, I am using the format Province -> City -> Suburb. Now as far as I can tell my code is fine, but when the execution hits the section where I add a listener to the "City" select, it throws an error saying the following:</p> <p><code>The child with the name "physicalCity" does not exist.</code></p> <p>This obviously happens when I try and add an event listener to the newly created field, thus adding an event listener to an element created by an event listener? </p> <p>A section of the code is below... What am I doing wrong? Any help would be very much appreciated!</p> <pre><code>public function buildForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('schoolName') -&gt;add('physicalProvince', 'entity', array( 'mapped' =&gt; false, 'class' =&gt; 'MY\MainBundle\Entity\Province', 'empty_value' =&gt; 'Select a province', 'attr' =&gt; array( 'class' =&gt; 'province', 'data-show' =&gt; 'physical-city', ) )); /* * For the physical cities */ $physicalCityModifier = function(FormInterface $form, Province $province = null) { if (null !== $province) $cities = $province-&gt;getCities(); else $cities = array(); $form-&gt;add('physicalCity', 'entity', array( 'mapped' =&gt; false, 'class' =&gt; 'MY\MainBundle\Entity\City', 'empty_value' =&gt; 'Select a province first', 'choices' =&gt; $cities, 'attr' =&gt; array( 'class' =&gt; 'city physical-city', 'data-show' =&gt; 'physical-suburb' ) )); }; $builder-&gt;addEventListener( FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($physicalCityModifier) { $data = $event-&gt;getData(); if (is_object($data-&gt;getPhysicalSuburb())) $province = $data-&gt;getPhysicalSuburb()-&gt;getCity()-&gt;getProvince(); else $province = null; $physicalCityModifier($event-&gt;getForm(), $province); } ); $builder-&gt;get('physicalProvince')-&gt;addEventListener( FormEvents::POST_SUBMIT, function (FormEvent $event) use ($physicalCityModifier) { $province = $event-&gt;getForm()-&gt;getData(); $physicalCityModifier($event-&gt;getForm()-&gt;getParent(), $province); } ); /* * For the physical suburbs */ $physicalSuburbModifier = function(FormInterface $form, City $city = null) { if (null !== $city) $suburbs = $city-&gt;getSuburbs(); else $suburbs = array(); $form-&gt;add('physicalSuburb', null, array( 'choices' =&gt; $suburbs, 'empty_value' =&gt; 'Select a city first', 'attr' =&gt; array( 'class' =&gt; 'physical-suburb' ), )); }; $builder-&gt;addEventListener( FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($physicalSuburbModifier) { $data = $event-&gt;getData(); if (is_object($data-&gt;getCity())) $city = $data-&gt;getCity(); else $city = null; $physicalSuburbModifier($event-&gt;getForm(), $city); } ); $builder-&gt;get('physicalCity')-&gt;addEventListener( FormEvents::POST_SUBMIT, function(FormEvent $event) use ($physicalSuburbModifier) { $city = $event-&gt;getForm()-&gt;getData(); $physicalSuburbModifier($event-&gt;getForm()-&gt;getParent(), $city); } ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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