Symfony 2 形式,嵌入集合中嵌入集合 [英] Symfony 2 forms, embedding collection in embedded collection

查看:28
本文介绍了Symfony 2 形式,嵌入集合中嵌入集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已将问题设置为主题表单中的嵌入式集合,由于 食谱条目.

当我尝试开发它以在问题表单中嵌入一系列回答表单时,我遇到了一个问题.

包含顶层原型表单的 data-prototype 属性具有完整的表单深度,因此包含了 Question 和 Answer 的原型.但它对每个级别使用相同的占位符 __name__.

<div class="control-group"><label for="topic_questions___name___questionText" class="control-label">问题</label><div class="form-row-errors"></div><div class="控件"><textarea id="topic_questions___name___questionText" name="topic[questions][__name__][questionText]" required="required" class="input-block-level"></textarea>

<div class="control-group"><label class="control-label">answers</label><div class="控件"><div id="topic_questions___name___answers" data-prototype="<div class="control-group"><label class="control-label&quot;>__name__label__</label><div class="controls&gt;&lt;div id=&quot;topic_questions___name___answers___name__&&gt;<div class=&quot;control-group&gt;<label for=&quot;topic_questions___name___answers___name___answerText&quot; class=&quot;control-label&quot;&gt;option</label><div class="form-row-errors&gt;</div><divclass="controls"><input type="text" id="topic_questions___name___answers___name___answerText" name="topic[questions][__name__][answers][__name__][answerText]" required="required&quot; maxlength="255&quot;/&gt;&lt;/div>&lt;/div><输入类型=隐藏"id="topic_questions___name___answers___name___sortOrder&quot;name="topic[questions][__name__][answers][__name__][sortOrder]"/&gt;&lt;/div></div>&lt;/div>"></div>

您可以在底部看到很长的一行,我猜这是答案形式的原型-原型 (!).我无法看到只替换与问题相关的 [__name__] 占位符,而不是与 Answer 相关的占位符.

做正常的事情

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

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

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

<label class="control-label">1label__</label><div class="控件"><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="控件"><input type="text" id="topic_questions_1_answers_1_answerText" name="topic[questions][1][answers][1][answerText]" required="required" maxlength="255"/>

如您所见,__name__ 占位符根本没有功能 - 在创建问题表单时,它已被替换为问题表单的计数.

有没有可能用Symfony提供的机制来实现这种多深度嵌入的集合?

只要它尝试为每个级别"使用相同的占位符,那么我就看不到如何了.

解决方案

你至少使用 Symfony 2.1 吗?如果是这样,您可以使用属性 prototype_name:

更改 __name__ 标签

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

在您的表单中:

->add('answers', 'collection', array('类型' =>新的答案类型(),'allow_add' =>真的,'prototype_name' =>'another_name'))

I have a data structure where a Topic has many Questions (one to many), and a Question has many Answers (one to many).

I have set up Questions as an embedded collection in the Topic form, and I have it all working 100% thanks to the cookbook entry.

When I try to develop this to embed a collection of Answer forms in the Question form then I run in to a problem.

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 __name__ for each level.

<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>

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 [__name__] placeholders and not the Answer related ones.

Doing the normal

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

when creating a real instance of a Question form of course replaces all instances of __name__ with the same value, so when the data-prototype is created for the Answer form, it has already had all the placeholders replaced.

This is what the data-prototype looks like for the Answer form, when I have clicked to add a real Question form

<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>

As you can see, the __name__ placeholder doesnt feature at all - it was already replaced with the count for the Question form when the question form was created.

Is it possible to achieve this kind of multiple depth embedded collection with the mechanism Symfony provides?

As long as it tries to use the same placeholder for each 'level' then I can not see how.

解决方案

Are you using at least Symfony 2.1? If so, you can change the __name__ label with the property prototype_name:

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

In your form:

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

这篇关于Symfony 2 形式,嵌入集合中嵌入集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆