表单与集合相同的实体类型

时间:2014-05-06 07:49:56

标签: symfony

我想要一个我的实体«X»的表格。该实体拥有OneToMany与许多“X”类型实体的关系。这是父母的关系< - >孩子。

当我简单地创建表单时,它可以正常工作。

class XType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add("name",     "text",       array("label" => "Nom"))
            ->add("children", "collection", array(
                "type"         => new XType(),
                "by_reference" => false));
    }
}

然后,我想在我的集合中使用选项«allow_add»轻松添加新实体,并使用原型添加javascript。这是我的表格,带有«allow_add»选项。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add("name",     "text",       array("label" => "Nom"))
        ->add("children", "collection", array(
            "type"         => new XType(),
            "allow_add"    => true,
            "by_reference" => false));
}

当我使用或不使用原型执行时,我遇到了Web服务器错误。这是XDebug因为递归调用太大而踢我的请求。有级联通话。

解决问题的最佳解决方案是什么?

2 个答案:

答案 0 :(得分:2)

我无法确切地说出你的问题在哪里,但应该在javascript和收集原型的某个地方。此外,当您允许添加项目时,通常也允许allow_delete。

看看我的例子:

$builder->add('book',   'collection',   array(
                                                'type' => new BookType(),
                                                 'allow_add' => true,
                                                 'allow_delete' => true,
                                                 'prototype_name' => '__prototype__',
                                                 'by_reference' => false,
                                                 'error_bubbling' => false
                                               );

在您呈现表单的模板中,包含以下内容:

{% block javascript %}
    {{ parent() }}
    {% javascripts
    '@ProjectSomeBundle/Resources/public/js/form/collection.js'
    %}
    <script type="text/javascript" src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

collection.js文件包含:

$(function($) {
    $addButton = $('button.add');
    $collection = $addButton.parent().children().first();

    $addButton.click(function () {
        var prototype = $($collection.data('prototype').replace(/__prototype__/g, function() { return (new Date()).getTime(); }));
        prototype.appendTo($collection.children('ul'));
        });

    $('body').on('click', 'button.remove', function () {
        $(this).parent().remove();
    });
});

此外,您应该使用那些传递给twig的自定义集合小部件。没什么好看的,它会被呈现为带有项目的无序列表,以便让项目易于访问。

config.yml:

twig:
    form:
        resources:
            - 'ProjectSomeBundle:Form:fields.html.twig'

fields.html.twig:

{% extends 'form_div_layout.html.twig' %}

{% block collection_widget %}
    {% import  "ProjectSomeBundle:Macro:macro.html.twig" as macro %}
    {% spaceless %}
        <div class="collection">
            {% if prototype is defined %}
                {% set attr = attr|merge({'data-prototype': block('prototype_widget') }) %}
            {% endif %}
            <div {{ block('widget_container_attributes') }}>
                <ul>
                    {% for row in form %}
                        {{ macro.collection_item_widget(row) }}
                    {% endfor %}
                </ul>
                {{ form_rest(form) }}
            </div>
            <div class="clear"></div>
            <button type="button" class="add">{% trans %}Add{% endtrans %}</button>
        </div>
        <div class="clear"></div>
    {% endspaceless %}
{% endblock collection_widget %}

{% block prototype_widget %}
    {% spaceless %}
        {{ macro.collection_item_widget(prototype) }}
    {% endspaceless %}
{% endblock prototype_widget %}

你可以注意到它使用宏,所以这里是:

macro.html.twig

{% macro collection_item_widget(fields) %}
    <li>
        {% set fieldNum = 1 %}
        {% for field in fields %}
            <div class="field_{{ fieldNum }}">
                {{ form_label(field) }}
                {{ form_errors(field) }}
                {{ form_widget(field) }}
            </div>
            {% set fieldNum = fieldNum + 1 %}
        {% endfor %}
        <button type="button" class="remove">{% trans %}Delete{% endtrans %}</button>
        <div class="clear"></div>
    </li>
{% endmacro %}

这是一个完整的例子,我希望你发现它很有用并适合你。

答案 1 :(得分:1)

我遇到了同样的问题(由于太多的递归调用,我的网络服务器也崩溃了)。我的快速解决方法是简单地创建一个不包含递归字段的虚拟(克隆)类型(这对我有用,因为我只对第一级孩子感兴趣)。

因此,如果此方案适用于您,您可以按如下方式更改代码:

class XType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add("name",     "text",       array("label" => "Nom"))
            ->add("children", "collection", array(
                "type"         => new XTypeWithoutChildren(),
                "by_reference" => false));
    }
}

class XTypeWithoutChildren extends XType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add("name",     "text",       array("label" => "Nom"));
    }
}
相关问题