【问题标题】:Symfony 2 forms, embedding collection in embedded collectionSymfony 2 形式,在嵌入集合中嵌入集合
【发布时间】:2012-11-03 10:44:08
【问题描述】:

我有一个数据结构,其中一个主题有很多问题(一对多),一个问题有很多答案(一对多)。

我已将问题设置为主题表单中的嵌入式集合,感谢cookbook entry,我可以 100% 工作。

当我尝试开发它以在问题表单中嵌入一组答案表单时,我遇到了一个问题。

包含顶层原型表单的 data-prototype 属性具有完整的表单深度,因此包括问题和答案的原型。但它对每个级别使用相同的占位符__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);

在创建 Question 表单的真实实例时,当然会将所有 __name__ 实例替换为相同的值,因此在为 Answer 表单创建数据原型时,它已经替换了所有占位符。

当我点击添加一个真正的问题表单时,这就是答案表单的数据原型的样子

<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 提供的机制来实现这种多深度的嵌入集合?

只要它尝试对每个“级别”使用相同的占位符,我就看不到怎么做。

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    您至少使用 Symfony 2.1 吗?如果是这样,您可以使用属性prototype_name 更改__name__ 标签:

    http://symfony.com/doc/master/reference/forms/types/collection.html#prototype-name

    在您的表单中:

    ->add('answers', 'collection', array(
        'type' => new AnswerType(),
        'allow_add' => true,
        'prototype_name' => 'another_name'
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      相关资源
      最近更新 更多