title: How to handle Ajax forms with collections in Symfony2.3
body: <p>So I have this form that I'd like to submit with Ajax.
  Normally this is quite easy to do, but as it is now it doesn't handle the embedded collection the right way, which is too bad.</p>
  
  <p>I have a form where you can create a question with a collection of answers.
  The form is created as following:</p>
  
  <pre><code>    {{ form_start(form, {'attr':{'class': 'form-horizontal', 'id': 'form'}}) }}
          &lt;div class="form-group"&gt;
              {{ form_label(form.question, null, { 'label_attr': {'class': 'col-lg-2 control-label'} }) }}
              &lt;div class="col-lg-5"&gt;
                  {{ form_widget(form.question, { 'attr': {'class': 'form-control'} }) }}
              &lt;/div&gt;
          &lt;/div&gt;
          {% for answer in form.answers %}
              &lt;div class="form-group"&gt;
                  {{ form_label(answer.answer, 'Answer '~loop.index, { 'label_attr': {'class': 'col-lg-2 control-label'} }) }}
                  &lt;div class="col-lg-5"&gt;
                      {{ form_widget(answer.answer, { 'attr': {'class': 'form-control'} }) }}
                  &lt;/div&gt;
              &lt;/div&gt;
          {% endfor %}
          &lt;div class="col-lg-8"&gt;&lt;input class="btn btn-primary pull-right" type="submit" /&gt;&lt;/div&gt;
      {{ form_end(form) }}
  </code></pre>
  
  <p>Now to submit the form, I've got this little javascript in place:</p>
  
  <pre><code>$('#form').submit( function() {
      $.ajax({
          type: "POST",
          url: "{{ url('questionCreateSubmit') }}",
          data: $(this).serialize(),
          success: function(data) {
              alert(data); 
          }
      });
  
      return false; 
  });
  </code></pre>
  
  <p>Now in my controller I have this to test if it handles it right:</p>
  
  <pre><code>/**
   * @Route("/questions/create/submit", name="questionCreateSubmit")
   */
  public function questionCreateSubmitAction(Request $request)
  {
      $question = new Question();
  
      $form = $this-&gt;createForm(new QuestionType(), $question);
      $form-&gt;handleRequest($request);
      die( var_dump( $form-&gt;getData() ) );
  </code></pre>
  
  <p>But it only sets the question attribute and ignores the collection of answers, this is what returns:</p>
  
  <pre><code>(
      [id:Acme\DemoBundle\Entity\Question:private] =&gt; 
      [question:Acme\DemoBundle\Entity\Question:private] =&gt; dfsdf
      [answers:Acme\DemoBundle\Entity\Question:private] =&gt; Doctrine\Common\Collections\ArrayCollection Object
      (
          [_elements:Doctrine\Common\Collections\ArrayCollection:private] =&gt; Array
              (
              )
      )
  )
  </code></pre>
  
  <p>When I check the $_POST variables by dumping those, it clearly shows an array structure like this:</p>
  
  <pre><code>(
      [question] =&gt; Array
          (
              [question] =&gt; dfsdf
              [answers] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [answer] =&gt; sdfsdf
                          )
                      [1] =&gt; Array
                          (
                              [answer] =&gt; dsfds
                          )
                      [2] =&gt; Array
                          (
                              [answer] =&gt; fsdfsdf
                          )
                  )
              [_token] =&gt; cf8781508d9002504455472d676acb1073888e67
          )
  )
  </code></pre>
  
  <p>Is there a way that the collections will be filled in automatically or do I have to make due with doing it all myself by iterating over the answers array and filling it in the question object?</p>
  
  <p>Help would be much appreciated!</p>