title: Symfony 2 forms, embedding collection in embedded collection
body: <p>I have a data structure where a Topic has many Questions (one to many), and a Question has many Answers (one to many).</p>
  
  <p>I have set up Questions as an embedded collection in the Topic form, and I have it all working 100% thanks to the <a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="noreferrer">cookbook entry</a>.</p>
  
  <p>When I try to develop this to embed a collection of Answer forms in the Question form then I run in to a problem.</p>
  
  <p>The data-prototype attribute that contains the prototype form at the top level has the full depth of form in it, so includes the prototype for both Question and Answer. But it uses the same placeholder <code>__name__</code> for each level.</p>
  
  <pre><code>&lt;div id="topic_questions___name__"&gt;
  &lt;div class="control-group"&gt;
      &lt;label for="topic_questions___name___questionText" class="control-label"&gt;question&lt;/label&gt;
      &lt;div class="form-row-errors"&gt;&lt;/div&gt;
      &lt;div class="controls"&gt;
          &lt;textarea id="topic_questions___name___questionText" name="topic[questions][__name__][questionText]" required="required" class="input-block-level"&gt;&lt;/textarea&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="control-group"&gt;
      &lt;label class="control-label"&gt;answers&lt;/label&gt;
      &lt;div class="controls"&gt;
          &lt;div id="topic_questions___name___answers"     data-prototype="&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&amp;lt;label class=&amp;quot;control-label&amp;quot;&amp;gt;__name__label__&amp;lt;/label&amp;gt;&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&amp;lt;div id=&amp;quot;topic_questions___name___answers___name__&amp;quot;&amp;gt;&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&amp;lt;label for=&amp;quot;topic_questions___name___answers___name___answerText&amp;quot; class=&amp;quot;control-label&amp;quot;&amp;gt;option&amp;lt;/label&amp;gt;&amp;lt;div class=&amp;quot;form-row-errors&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; id=&amp;quot;topic_questions___name___answers___name___answerText&amp;quot; name=&amp;quot;topic[questions][__name__][answers][__name__][answerText]&amp;quot; required=&amp;quot;required&amp;quot; maxlength=&amp;quot;255&amp;quot; /&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;input type=&amp;quot;hidden&amp;quot; id=&amp;quot;topic_questions___name___answers___name___sortOrder&amp;quot; name=&amp;quot;topic[questions][__name__][answers][__name__][sortOrder]&amp;quot; /&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;"&gt;&lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>You can see the really long line at the bottom, which I guess is the prototype-prototype (!) for the answer form. There is no way that I can see to only replace the question related <code>[__name__]</code> placeholders and not the Answer related ones. </p>
  
  <p>Doing the normal</p>
  
  <pre><code>var newForm = prototype.replace(/__name__/g, collectionHolder.children().length);
  </code></pre>
  
  <p>when creating a real instance of a Question form of course replaces all instances of <code>__name__</code> with the same value, so when the data-prototype is created for the Answer form, it has already had all the placeholders replaced.</p>
  
  <p>This is what the data-prototype looks like for the Answer form, when I have clicked to add a real Question form</p>
  
  <pre><code>&lt;div class="control-group"&gt;
  &lt;label class="control-label"&gt;1label__&lt;/label&gt;
  &lt;div class="controls"&gt;
      &lt;div id="topic_questions_1_answers_1"&gt;
          &lt;div class="control-group"&gt;
              &lt;label for="topic_questions_1_answers_1_answerText" class="control-label"&gt;option&lt;/label&gt;
              &lt;div class="form-row-errors"&gt;&lt;/div&gt;
              &lt;div class="controls"&gt;
                  &lt;input type="text" id="topic_questions_1_answers_1_answerText" name="topic[questions][1][answers][1][answerText]" required="required" maxlength="255" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>As you can see, the <code>__name__</code> placeholder doesnt feature at all - it was already replaced with the count for the Question form when the question form was created.</p>
  
  <p>Is it possible to achieve this kind of multiple depth embedded collection with the mechanism Symfony provides?</p>
  
  <p>As long as it tries to use the same placeholder for each 'level' then I can not see how.</p>