A form with a collection of the same entity type

I need a form for my "X" entity. This object has OneToMany relationships with many objects of type "X". This is parenting with โ†” children parents.

When I just create my form, it works.

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));
    }
}

Then I would like to easily add a new entity to my collection with the "allow_add" option and use the prototype to add to javascript. This is my form with the parameter "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));
}

When I run with or without a prototype call, I have a web server error. This is XDebug that launches my request because the recursive call is too large. There is a cascading call.

What is the best solution to solve my problem?

0
source share
2 answers

, , - 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();
    });
});

, , . , , .

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

, , .

+2

( - , - ). () , ( , ).

, , :

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"));
    }
}
+1

Source: https://habr.com/ru/post/1539501/


All Articles