<p dir="auto">In my web application I commonly use the pattern of mapping a collection of fields to an array on one of my entities.  As of upgrading to 2.3 today I've noticed that if a collection has only one field and that is empty, then an UnexpectedTypeException is thrown on line 76 of EventListener/ResizeFormListener.php.  I tried adding an empty_data option of either null or array to both the collections and the field, but an empty string was still returned.</p>
<p dir="auto">Also, the data is being posted as an array <code class="notranslate">application[person][citizenship][countries][0]</code>, which class would be squishing this down to a string?</p>
<p dir="auto">This is an example of one of my collections, here the aaws field is just a choice that gets its values from a webservice.</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$builder-&gt;add('countries', 'collection', array('type' =&gt; 'aaws', 'options' =&gt; array(
                                                                 'label' =&gt; 'Citizenship Country', 
                                                                 'app_data_attr' =&gt; 'countries', 
                                                                 'empty_value' =&gt; 'Choose an option',
                                                                 'empty_data'  =&gt; null,
                                                                 'required' =&gt; false,
                                                                 'preferred_choices' =&gt; array('USA'),
                                                                 'attr' =&gt; array('class' =&gt; 'gfu-required-field')
                                                                  ),
                                                                 'allow_add' =&gt; true,
                                                                 'allow_delete' =&gt; true,
                                                                 'prototype' =&gt; true,
                                                                 'error_bubbling' =&gt; false,
                                                                 'empty_data' =&gt; null,
                                                  )
"><pre class="notranslate"><span class="pl-s1"><span class="pl-c1">$</span>builder</span>-&gt;add(<span class="pl-s">'countries'</span>, <span class="pl-s">'collection'</span>, <span class="pl-en">array</span>(<span class="pl-s">'type'</span> =&gt; <span class="pl-s">'aaws'</span>, <span class="pl-s">'options'</span> =&gt; <span class="pl-en">array</span>(
                                                                 <span class="pl-s">'label'</span> =&gt; <span class="pl-s">'Citizenship Country'</span>, 
                                                                 <span class="pl-s">'app_data_attr'</span> =&gt; <span class="pl-s">'countries'</span>, 
                                                                 <span class="pl-s">'empty_value'</span> =&gt; <span class="pl-s">'Choose an option'</span>,
                                                                 <span class="pl-s">'empty_data'</span>  =&gt; <span class="pl-c1">null</span>,
                                                                 <span class="pl-s">'required'</span> =&gt; <span class="pl-c1">false</span>,
                                                                 <span class="pl-s">'preferred_choices'</span> =&gt; <span class="pl-en">array</span>(<span class="pl-s">'USA'</span>),
                                                                 <span class="pl-s">'attr'</span> =&gt; <span class="pl-en">array</span>(<span class="pl-s">'class'</span> =&gt; <span class="pl-s">'gfu-required-field'</span>)
                                                                  ),
                                                                 <span class="pl-s">'allow_add'</span> =&gt; <span class="pl-c1">true</span>,
                                                                 <span class="pl-s">'allow_delete'</span> =&gt; <span class="pl-c1">true</span>,
                                                                 <span class="pl-s">'prototype'</span> =&gt; <span class="pl-c1">true</span>,
                                                                 <span class="pl-s">'error_bubbling'</span> =&gt; <span class="pl-c1">false</span>,
                                                                 <span class="pl-s">'empty_data'</span> =&gt; <span class="pl-c1">null</span>,
                                                  )</pre></div>
<p dir="auto">Here is the plain text of my stack trace</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[1] Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type &quot;array or (\Traversable and \ArrayAccess)&quot;, &quot;string&quot; given
    at n/a
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php line 76

    at Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener-&gt;preSetData(object(FormEvent))
        in  line 

    at call_user_func(array(object(ResizeFormListener), 'preSetData'), object(FormEvent))
        in /vagrant/app/cache/dev/classes.php line 1676

    at Symfony\Component\EventDispatcher\EventDispatcher-&gt;doDispatch(array(array(object(ResizeFormListener), 'preSetData')), 'form.pre_set_data', object(FormEvent))
        in /vagrant/app/cache/dev/classes.php line 1609

    at Symfony\Component\EventDispatcher\EventDispatcher-&gt;dispatch('form.pre_set_data', object(FormEvent))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php line 42

    at Symfony\Component\EventDispatcher\ImmutableEventDispatcher-&gt;dispatch('form.pre_set_data', object(FormEvent))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 321

    at Symfony\Component\Form\Form-&gt;setData('')
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 59

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapDataToForms(object(Ethnicity), object(RecursiveIteratorIterator))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 375

    at Symfony\Component\Form\Form-&gt;setData(object(Ethnicity))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 59

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapDataToForms(object(Person), object(RecursiveIteratorIterator))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 375

    at Symfony\Component\Form\Form-&gt;setData(object(Person))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 59

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapDataToForms(object(Application), object(RecursiveIteratorIterator))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 375

    at Symfony\Component\Form\Form-&gt;setData(object(Application))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 467

    at Symfony\Component\Form\Form-&gt;initialize()
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 229

    at Symfony\Component\Form\FormBuilder-&gt;getForm()
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39

    at Symfony\Component\Form\FormFactory-&gt;create(object(ApplicationType), object(Application), array('validation_groups' =&gt; array('Default'), 'gfu_submit' =&gt; false))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 163

    at Symfony\Bundle\FrameworkBundle\Controller\Controller-&gt;createForm(object(ApplicationType), object(Application), array('validation_groups' =&gt; array('Default'), 'gfu_submit' =&gt; false))
        in /vagrant/src/Gfu/AppBundle/Controller/AppController.php line 472

    at Gfu\AppBundle\Controller\AppController-&gt;editAction('72', object(Request))
        in  line 

    at call_user_func_array(array(object(AppController), 'editAction'), array('72', object(Request)))
        in /vagrant/app/bootstrap.php.cache line 2774

    at Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(object(Request), '1')
        in /vagrant/app/bootstrap.php.cache line 2748

    at Symfony\Component\HttpKernel\HttpKernel-&gt;handle(object(Request), '1', true)
        in /vagrant/app/bootstrap.php.cache line 2878

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel-&gt;handle(object(Request), '1', true)
        in /vagrant/app/bootstrap.php.cache line 2179

    at Symfony\Component\HttpKernel\Kernel-&gt;handle(object(Request))
        in /vagrant/web/app_dev.php line 28"><pre class="notranslate"><code class="notranslate">[1] Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given
    at n/a
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php line 76

    at Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener-&gt;preSetData(object(FormEvent))
        in  line 

    at call_user_func(array(object(ResizeFormListener), 'preSetData'), object(FormEvent))
        in /vagrant/app/cache/dev/classes.php line 1676

    at Symfony\Component\EventDispatcher\EventDispatcher-&gt;doDispatch(array(array(object(ResizeFormListener), 'preSetData')), 'form.pre_set_data', object(FormEvent))
        in /vagrant/app/cache/dev/classes.php line 1609

    at Symfony\Component\EventDispatcher\EventDispatcher-&gt;dispatch('form.pre_set_data', object(FormEvent))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php line 42

    at Symfony\Component\EventDispatcher\ImmutableEventDispatcher-&gt;dispatch('form.pre_set_data', object(FormEvent))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 321

    at Symfony\Component\Form\Form-&gt;setData('')
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 59

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapDataToForms(object(Ethnicity), object(RecursiveIteratorIterator))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 375

    at Symfony\Component\Form\Form-&gt;setData(object(Ethnicity))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 59

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapDataToForms(object(Person), object(RecursiveIteratorIterator))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 375

    at Symfony\Component\Form\Form-&gt;setData(object(Person))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 59

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapDataToForms(object(Application), object(RecursiveIteratorIterator))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 375

    at Symfony\Component\Form\Form-&gt;setData(object(Application))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 467

    at Symfony\Component\Form\Form-&gt;initialize()
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 229

    at Symfony\Component\Form\FormBuilder-&gt;getForm()
        in /vagrant/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39

    at Symfony\Component\Form\FormFactory-&gt;create(object(ApplicationType), object(Application), array('validation_groups' =&gt; array('Default'), 'gfu_submit' =&gt; false))
        in /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 163

    at Symfony\Bundle\FrameworkBundle\Controller\Controller-&gt;createForm(object(ApplicationType), object(Application), array('validation_groups' =&gt; array('Default'), 'gfu_submit' =&gt; false))
        in /vagrant/src/Gfu/AppBundle/Controller/AppController.php line 472

    at Gfu\AppBundle\Controller\AppController-&gt;editAction('72', object(Request))
        in  line 

    at call_user_func_array(array(object(AppController), 'editAction'), array('72', object(Request)))
        in /vagrant/app/bootstrap.php.cache line 2774

    at Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(object(Request), '1')
        in /vagrant/app/bootstrap.php.cache line 2748

    at Symfony\Component\HttpKernel\HttpKernel-&gt;handle(object(Request), '1', true)
        in /vagrant/app/bootstrap.php.cache line 2878

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel-&gt;handle(object(Request), '1', true)
        in /vagrant/app/bootstrap.php.cache line 2179

    at Symfony\Component\HttpKernel\Kernel-&gt;handle(object(Request))
        in /vagrant/web/app_dev.php line 28
</code></pre></div>
<p dir="auto">Hi guys,</p>
<p dir="auto">I just notice that, when I send a non array data to a collection form field the <code class="notranslate">ResizeFormListener</code> throws an <code class="notranslate">UnexpectedTypeException</code> before the form validation.</p>
<p dir="auto">Here is the action</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="public function indexAction(Request $request)
{
    $form = $this-&gt;get('form.factory')-&gt;createNamed('', new FooType, null, [
        'method' =&gt; 'GET'
    ]);

    $request-&gt;query-&gt;set('emails', 'a');

    // Throws an exception
    $form-&gt;handleRequest($request);

    if ($form-&gt;isValid()) {
        die(var_dump('valid'));
    }

    die(var_dump($form-&gt;getErrorsAsString()));
}
"><pre class="notranslate"><code class="notranslate">public function indexAction(Request $request)
{
    $form = $this-&gt;get('form.factory')-&gt;createNamed('', new FooType, null, [
        'method' =&gt; 'GET'
    ]);

    $request-&gt;query-&gt;set('emails', 'a');

    // Throws an exception
    $form-&gt;handleRequest($request);

    if ($form-&gt;isValid()) {
        die(var_dump('valid'));
    }

    die(var_dump($form-&gt;getErrorsAsString()));
}

</code></pre></div>
<p dir="auto">And the form</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class FooType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder-&gt;add('emails', 'collection', [
            'type' =&gt; 'email'
        ]);
    }

    public function getName()
    {
        return 'foo';
    }
}"><pre class="notranslate"><code class="notranslate">class FooType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder-&gt;add('emails', 'collection', [
            'type' =&gt; 'email'
        ]);
    }

    public function getName()
    {
        return 'foo';
    }
}
</code></pre></div>
<p dir="auto">Is there anything that I can do to avoid this? By this I mean to get a non valid form instead of an Exception...</p>
<p dir="auto">I don't really get why <a href="https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php#L147">https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php#L147</a> should throws an Exception instead of doing a <code class="notranslate">$data = [];</code> or something.</p>
<p dir="auto">What do you think?</p>