title: Symfony 2 collection type, validation doesn't work and "adder" method called with NULL value?
body: <p>My <code>Product</code> <strong>entity</strong> has an "adder" method (explained <a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow">here</a>) to add entities of type <code>ProductVariant</code>:</p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="ProductVariant", mappedBy="product", 
   *     cascade={"persist"})
   * @Assert\Valid()
   *
   * @var ArrayCollection
   */
  protected $variants;
  
  /**
   * @param ProductVariant $variant
   * @return $this
   */
  public function addVariant(ProductVariant $variant)
  {
      if (!$this-&gt;variants-&gt;contains($variant)) {
          $this-&gt;variants-&gt;add($variant-&gt;setProduct($this));
      }
  
      return $this;
  }
  </code></pre>
  
  <p><code>ProductType</code> <strong>form</strong> type is adding a collection of <code>ProductVariantType</code>:</p>
  
  <pre><code>$builder-&gt;add('variants', 'collection', array(
      'type' =&gt; new ProductVariantType(),
      'allow_add'    =&gt; true,
      'allow_delete' =&gt; true,
      'by_reference' =&gt; false, // ensure addVariant and removeVariant calls
  ));
  </code></pre>
  
  <p><strong>Problem #1 (validation)</strong>: when I add a new variant (using add button/JavaScript) the new embedded form isn't validated. If I left blank all fields (i.e some of them are required) validation passes. <em>Validation occurs only filling some fields</em>.</p>
  
  <p><strong>Problem #2 (<code>addVariant</code> call)</strong>: <code>addVariant</code> is called with <code>NULL</code>, probably because the problem #1.</p>
  
  <p>Some <code>ProductVariant</code> fields:</p>
  
  <pre><code>/**
   * @ORM\Column(unique=true)
   * @Assert\NotBlank()
   *
   * @var string
   */
  protected $code;
  
  /**
   * @ORM\Column(type="integer")
   * @Assert\NotBlank()
   * @Assert\Range(min=0, max=2147483647)
   *
   * @var string
   */
  protected $quantity;
  </code></pre>
  
  <p>I'm using the following JavaScript to add/remove variants:</p>
  
  <pre><code>&lt;script&gt;
      $(function () {  
          $('.btn-add').click(function(e) {
              e.preventDefault();
  
              var holder = $('#' + $(this).attr('data-target')),
                  proto  = holder.attr('data-prototype')
                              .replace(/__name__/g, holder.children().length - 1);
  
             holder.append(proto);
          });
  
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>And of course, the prototype:</p>
  
  <pre><code>&lt;div class="tab-pane" id="variants" 
      data-prototype="{{ form_widget(form.variants.vars.prototype)|e }}"&gt;
      &lt;div class="panel"&gt;
          &lt;div class="panel-body"&gt;
              &lt;a href="#" class="btn-add" data-target="variants"&gt;Add variant&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      {% for variant in form.variants %}
          {{ form_widget(variant) }}
      {% endfor %}
  &lt;/div&gt;
  </code></pre>
  
  <p>Finally the (simple) controller code:</p>
  
  <pre><code>public function editAction(Request $request, Product $product)
  {
      $form = $this-&gt;createForm('product', $product)
          -&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $this-&gt;getDoctrine()-&gt;getManager()
              -&gt;flush();
  
          return $this-&gt;redirect(
              $this-&gt;generateUrl(
                  'me_test_product_edit',
                  array('id' =&gt; $product-&gt;getId())
              )
          );
      }
  
      return array('form' =&gt; $form-&gt;createView());
  }
  </code></pre>