<p dir="auto">I have a form type that incorporates another form type.</p>
<p dir="auto">I've added an event-listener to the sub-form but the listener is never executed.</p>
<p dir="auto">The first Form Type :</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class AFormType extends AbstractType {

   public function buildForm(FormBuilderInterface $builder, array $options) {
       $builder
               -&gt;add('aSubFormType', new SubFormType());
   }
//[...]
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">AFormType</span> <span class="pl-k">extends</span> <span class="pl-v">AbstractType</span> {

   <span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">buildForm</span>(<span class="pl-smi"><span class="pl-smi">FormBuilderInterface</span></span> <span class="pl-s1"><span class="pl-c1">$</span>builder</span>, <span class="pl-smi">array</span> <span class="pl-s1"><span class="pl-c1">$</span>options</span>) {
       <span class="pl-s1"><span class="pl-c1">$</span>builder</span>
               -&gt;<span class="pl-en">add</span>(<span class="pl-s">'aSubFormType'</span>, <span class="pl-k">new</span> <span class="pl-v">SubFormType</span>());
   }
<span class="pl-c">//[...]</span>
}</pre></div>
<p dir="auto">The second Form Type :</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class SubFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder-&gt;add(&quot;metier&quot;, &quot;text&quot;);

    $builder-&gt;addEventListener(
        \Symfony\Component\Form\FormEvents::PRE_SET_DATA,
        function(\Symfony\Component\Form\FormEvent $event){
        // Some Stuff never executed
    });        
    }
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">SubFormType</span> <span class="pl-k">extends</span> <span class="pl-v">AbstractType</span>
{

    <span class="pl-k">public</span> <span class="pl-k">function</span> <span class="pl-en">buildForm</span>(<span class="pl-smi"><span class="pl-smi">FormBuilderInterface</span></span> <span class="pl-s1"><span class="pl-c1">$</span>builder</span>, <span class="pl-smi">array</span> <span class="pl-s1"><span class="pl-c1">$</span>options</span>)
    {
    <span class="pl-s1"><span class="pl-c1">$</span>builder</span>-&gt;<span class="pl-en">add</span>("<span class="pl-s">metier</span>", "<span class="pl-s">text</span>");

    <span class="pl-s1"><span class="pl-c1">$</span>builder</span>-&gt;<span class="pl-en">addEventListener</span>(
        \<span class="pl-v">Symfony</span>\<span class="pl-v">Component</span>\<span class="pl-v">Form</span>\<span class="pl-v">FormEvents</span>::<span class="pl-c1">PRE_SET_DATA</span>,
        <span class="pl-k">function</span>(<span class="pl-smi"><span class="pl-smi">\<span class="pl-v">Symfony</span>\<span class="pl-v">Component</span>\<span class="pl-v">Form</span>\<span class="pl-v">FormEvent</span></span></span> <span class="pl-s1"><span class="pl-c1">$</span>event</span>){
        <span class="pl-c">// Some Stuff never executed</span>
    });        
    }
}</pre></div>
<p dir="auto">I don't know if this behaviour is the right or if this is a bug? And how I can use EventListner in sub-form ?</p>
<p dir="auto">Hi @bschussek</p>
<p dir="auto">I'm having an issue since I've updated my code to Symfony v2.3.1, I have an embed form defined as virtual and neither <code class="notranslate">FormEvents::PRE_SET_DATA</code> event nor <code class="notranslate">setData</code> method are called anymore.</p>
<p dir="auto">I wrote a test to be more explicit on the problem I have: <a href="https://gist.github.com/jeremyb/5764427">https://gist.github.com/jeremyb/5764427</a>. I ran this test on the Symfony 2.2 and 2.3 branches and I think there's a regression on <code class="notranslate">testVirtualAndEvents</code>.</p>
<table role="table">
<thead>
<tr>
<th>Test</th>
<th>2.2</th>
<th>2.3</th>
</tr>
</thead>
<tbody>
<tr>
<td>testVirtual</td>
<td>success</td>
<td>success</td>
</tr>
<tr>
<td>testVirtualWithCountry</td>
<td>fails</td>
<td>fails</td>
</tr>
<tr>
<td>testVirtualAndEvents</td>
<td>success</td>
<td>fails</td>
</tr>
</tbody>
</table>
<p dir="auto">I'm trying to understand the differences made on Form component between 2.2 and 2.3 and I see some PR that can be related to this issue:</p>
<ul dir="auto">
<li>I would have thought that PR <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="9705925" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/6573" data-hovercard-type="pull_request" data-hovercard-url="/symfony/symfony/pull/6573/hovercard" href="https://github.com/symfony/symfony/pull/6573">#6573</a> fixes the <code class="notranslate">setData</code> call for virtual forms but it seems that it's not the case and the latest comment of <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/Strate/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/Strate">@Strate</a> confirms that ;</li>
<li>the PR <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="13765895" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/7878" data-hovercard-type="pull_request" data-hovercard-url="/symfony/symfony/pull/7878/hovercard" href="https://github.com/symfony/symfony/pull/7878">#7878</a> introduces the <code class="notranslate">auto_initialize</code> option, so the <code class="notranslate">setData</code> isn't called anymore on children if I understand it correctly?</li>
</ul>
<p dir="auto">So I think my problem comes from the fact that <code class="notranslate">setData</code> isn't called for virtual forms and because <code class="notranslate">auto_initialize</code> prevents initialization of children (according to this the exception <code class="notranslate">Automatic initialization is only supported on root forms.</code>), so without initialization and data the <code class="notranslate">FormEvents::PRE_SET_DATA</code> event isn't called anymore. Is this correct?</p>
<p dir="auto">I'm not sure whether it's a bug or an evolution of the Form component but is there a new way to do this kind of code and adapt the behavior of a virtual form from the data?</p>
<p dir="auto">Let me know if you want me to commit my <code class="notranslate">FormVirtualTest.php</code> test somewhere, I can do a PR to your symfony fork if you want.</p>