title: Symfony 2 dynamic embedded forms, wrong (entity) type
body: <p>I have two entities with a on to many relationship.</p>
  
  <p>One "Entry" has n "Tags". Now I want to insert a new Entry with Tags. The form consist of a collection of "Tag" forms which can be dynamically added via javascript. One field is added by default in the controller code.</p>
  
  <p>When the form gets submittet and I iterate over the Tags for the Entry, only the first one (which has been added in the controller, not via javascript) has the proper entity type. The other ones are just arrays:</p>
  
  <pre><code>object(Entity\Type)[88]
     private 'id' =&gt; null
     private 'field' =&gt; null
     private 'type' =&gt; string 'S' (length=1)
  
  array (size=3)
    'field' =&gt; null
    'type' =&gt; string 'S' (length=1)
  </code></pre>
  
  <p>The "Entry" controller:</p>
  
  <pre><code>$entity = new Entry();
  $entity-&gt;getParameters()-&gt;add(new EntryTag());
  
  $form = $this-&gt;createForm($this-&gt;get('entryform'),$entity);
  
  if($request-&gt;getMethod() == 'POST'){
      $form-&gt;bindRequest($request);
  
      $formData = $form-&gt;getData();
      foreach ($formData-&gt;getParameters() as $par) {
          var_dump($par);
      }
  }
  </code></pre>
  
  <p>The "Entry" type:
  $this->tagType gets injeced in the constructor.</p>
  
  <pre><code>public function buildForm(FormBuilder $builder, array $options){
      $builder-&gt;add('parameters', 'collection', array(
          'type' =&gt; $this-&gt;tagType,
          'allow_add' =&gt; true,
          'allow_delete' =&gt; true,
          'by_reference' =&gt; false,
      ));
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Entity\Entry',
      ));
  }
  </code></pre>
  
  <p>The "Tag" type:</p>
  
  <pre><code>public function buildForm(FormBuilder $builder, array $options){
      $builder-&gt;add('field', 'text', array('label' =&gt; 'Key'));
      $builder-&gt;add('type', 'choice', array('label' =&gt; 'Type', 'required' =&gt; false, 'choices' =&gt; $this-&gt;getTypes()));
  }
  
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Entity/Tag',
      ));
  }
  </code></pre>
  
  <p>The javascript i use to add the fields:</p>
  
  <pre><code>var $childs = $container.find('.form-child');
  
  fieldIndindex++; 
  
  var template = $container
              .addClass('form-child')
      .attr('data-prototype')
      .replace(/\$\$name\$\$/g, fieldIndindex);
  
  if($childs.length == 0){
      $container.prepend($template);
  }else{
      $childs.last().after($template);
  }
  </code></pre>
  
  <p>So, why isn't the entity typ detected?</p>
  
  <p>Edit:</p>
  
  <p>When i add the "data_class" option to my "collection" field, it works. </p>
  
  <pre><code>$builder-&gt;add('parameters', 'collection', array(
          'type' =&gt; $this-&gt;parameterType,
          'allow_add' =&gt; true,
          'allow_delete' =&gt; true,
          'by_reference' =&gt; false,
          'options' =&gt; array(
              'data_class' =&gt; 'Entity/Tag',
          )
      ));
  </code></pre>
  
  <p>But setting it as default option in my "Tag" type should be enought. But why isn't it used?</p>