2012-11-03 32 views
7

嵌入收集我有一个数据结构,其中一个主题,有许多问题(一对多),以及一个问题有很多答案(一对多)。Symfony的2种形式,在嵌入式收集

我已经建立了问题作为主题的形式嵌入的集合,它所有的工作多亏了cookbook entry 100%我有。

当我尝试开发这种嵌入的答案形式的集合中的形式问题,然后我的一个问题运行英寸

包含在顶层的原型形式中有形式的全深度的数据原型属性,所以包括原型两个问题与答案。但它为每个级别使用相同的占位符__name__

<div id="topic_questions___name__"> 
<div class="control-group"> 
    <label for="topic_questions___name___questionText" class="control-label">question</label> 
    <div class="form-row-errors"></div> 
    <div class="controls"> 
     <textarea id="topic_questions___name___questionText" name="topic[questions][__name__][questionText]" required="required" class="input-block-level"></textarea> 
    </div> 
</div> 
<div class="control-group"> 
    <label class="control-label">answers</label> 
    <div class="controls"> 
     <div id="topic_questions___name___answers"  data-prototype="&lt;div class=&quot;control-group&quot;&gt;&lt;label class=&quot;control-label&quot;&gt;__name__label__&lt;/label&gt;&lt;div class=&quot;controls&quot;&gt;&lt;div id=&quot;topic_questions___name___answers___name__&quot;&gt;&lt;div class=&quot;control-group&quot;&gt;&lt;label for=&quot;topic_questions___name___answers___name___answerText&quot; class=&quot;control-label&quot;&gt;option&lt;/label&gt;&lt;div class=&quot;form-row-errors&quot;&gt;&lt;/div&gt;&lt;div class=&quot;controls&quot;&gt;&lt;input type=&quot;text&quot; id=&quot;topic_questions___name___answers___name___answerText&quot; name=&quot;topic[questions][__name__][answers][__name__][answerText]&quot; required=&quot;required&quot; maxlength=&quot;255&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;input type=&quot;hidden&quot; id=&quot;topic_questions___name___answers___name___sortOrder&quot; name=&quot;topic[questions][__name__][answers][__name__][sortOrder]&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"></div> 
    </div> 
</div> 

你可以看到在底部,我猜很长的线为原型样机(!)的答案形式。没有办法,我可以看到只更换问题相关[__name__]占位符,而不是答案相关的。

做正常

var newForm = prototype.replace(/__name__/g, collectionHolder.children().length); 

创建过程中的问题形式的一个真正的实例时替换的__name__所有实例使用相同的值,所以当应答形式创建数据的原型,它有已经取代了所有占位符。

这是数据的原型是什么样子的问答形式,在我点击添加一个真正的问题形式

<div class="control-group"> 
<label class="control-label">1label__</label> 
<div class="controls"> 
    <div id="topic_questions_1_answers_1"> 
     <div class="control-group"> 
      <label for="topic_questions_1_answers_1_answerText" class="control-label">option</label> 
      <div class="form-row-errors"></div> 
      <div class="controls"> 
       <input type="text" id="topic_questions_1_answers_1_answerText" name="topic[questions][1][answers][1][answerText]" required="required" maxlength="255" /> 
      </div> 
     </div> 
    </div> 
</div> 

正如你所看到的,在__name__占位犯规功能所有 - 当问题表单被创建时,它已经被替换为问题表单的计数。

Symfony提供的机制是否可以实现这种多深度嵌入式集合?

只要它试图为每个'级别'使用相同的占位符,那么我看不出如何。

回答