Symfony 2:如何处理表单中的嵌套集合

Symfony 2 : How to handle nested collections in forms

本文关键字:表单 集合 嵌套 处理 何处理 Symfony      更新时间:2023-09-26

我正在尝试创建一个包含嵌套集合的表单。我不知道如何处理JS部分以显示子集合。有人知道我该怎么做吗?

这是我的表单代码:

class ParentFormType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {   
        $builder
              ->add('case', 'choice', array(
                        'choices'   => array(
                            'case1'   =>  'case1',
                            'case2'   =>  'case2',
                            'case3'   =>  'case3',
                )))
            ->add ('subForm1', 'collection', array (
             'type' => new Sub1FormType(),
             'allow_add' => true,
             'allow_delete' => true,
             'by_reference' => false,
             'prototype' => true,
            ))
    ;
        $builder->add('save',"submit") ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
    }
    public function getName() {
        return 'formtestparenttype';
    }
}
class Sub1FormType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {   
        $builder
          ->add('fieldSub1',"text" )
          ->add ('childForm1', 'collection', array (
              'type' => new Sub2FormType,
              'allow_add' => true,
              'allow_delete' => true,
              'by_reference' => false,
              'prototype' => true,
          ))
          ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
    }
    public function getName() {
        return 'formtestsub1type';
    }
}
class Sub2FormType extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {   
        $builder
               ->add('fieldSub2',"text" )
          ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
    }
    public function getName() {
        return 'formtesttype';
    }
}

控制器 :

$form = $this->createForm(new ParentFormType() ) ;
return $this->render('MyBundle:Test:test.html.twig', array(
    'form' => $form->createView()
  ));

在这里; 树枝 + js 部分:

{% extends '::base.html.twig' %}
{% block content %}
{{ form_start(form) }} 
    <h3>Tags</h3>
    <ul class="collectionHolder" data-prototype="{{ form_widget(form.subForm1.vars.prototype)|e }}">
        {# iterate over each existing tag and render its only field: name #}
        {% for subForm1 in form.subForm1 %}
            <li>{{ form_row(subForm1) }} </li>
             <ul class="collectionHolder" data-prototype="{{ form_widget(subForm2.vars.prototype)|e }}">
            {%for subForm2 in subForm1.subForm2 %}
                <li>{{ form_row(subForm2) }}</li>
            {% endfor %}
        {% endfor %}
    </ul>
{{ form_end(form) }}

<script>
var $collectionHolder;
// setup an "add a tag" linkd
var $addTagLink = $('<a href="#" class="add_tag_link">Add</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
    function addTagForm($collectionHolder, $newLinkLi) 
    { 
        // Get the data-prototype explained earlier
        var prototype = $collectionHolder.data('prototype');
        // get the new index
        var index = $collectionHolder.data('index');
        // Replace '__name__' in the prototype's HTML to
        // instead be a number based on how many items we have
        var newForm = prototype.replace(/__name__/g, index);
        // increase the index with one for the next item
        $collectionHolder.data('index', index + 1);
        // Display the form in the page in an li, before the "Add a tag" link li
        var $newFormLi = $('<li></li>').append(newForm);
        $newLinkLi.before($newFormLi);
     }

    $collectionHolder = $('.collectionHolder');
    $collectionHolder.append($newLinkLi);
    // count the current form inputs we have (e.g. 2), use that as the new
    // index when inserting a new item (e.g. 2)
    $collectionHolder.data('index', $collectionHolder.find(':input').length);
    $addTagLink.on('click', function(e) {
        e.preventDefault();
        addTagForm($collectionHolder, $newLinkLi);
    });
});
</script>
{% endblock content %}

你的问题是示例javascript不是为了一次处理多个集合而编写的。

我写了一个单独的javascript文件,在处理这些表单集合时我总是包含该文件:

// js/form.collection.js
function FormCollection(div_id)
{
    // keep reference to self in all child functions
    var self=this;
    self.construct = function () {
        // set some shortcuts
        self.div = $('#'+div_id);
        self.div.data('index', self.div.find(':input').length);
        // add delete link to existing children
        self.div.children().each(function() {
            self.addDeleteLink($(this));
        });
        // add click event to the Add new button
        self.div.next().on('click', function(e) {
            // prevent the link from creating a "#" on the URL
            e.preventDefault();
            // add a new tag form (see next code block)
            self.addNew();
        });
    };
    /**
     * onClick event handler -- adds a new input
     */
    self.addNew = function () {
        // Get the data-prototype explained earlier
        var prototype = self.div.data('prototype');
        // get the new index
        var index = self.div.data('index');
        // Replace '__name__' in the prototype's HTML to
        // instead be a number based on how many items we have
        var newForm = prototype.replace(/__name__/g, index);
        // increase the index with one for the next item
        self.div.data('index', index + 1);
        // Display the form in the page in an li, before the "Add a tag" link li
        self.div.append($(newForm));
        // add a delete link to the new form
        self.addDeleteLink( $(self.div.children(':last-child')[0]) );
        // not a very nice intergration.. but when creating stuff that has help icons, 
        // the popovers will not automatically be instantiated
        //initHelpPopovers();
        return $(newForm);
    };
    /**
     * add Delete icon after input
     * @param Element row
     */
    self.addDeleteLink = function (row) {
        var $removeFormA = $('<a href="#" class="btn btn-danger" tabindex="-1"><i class="entypo-trash"></i></a>');
        $(row).find('select').after($removeFormA);
        row.append($removeFormA);
        $removeFormA.on('click', function(e) {
            // prevent the link from creating a "#" on the URL
            e.preventDefault();
            // remove the li for the tag form
            row.remove();
        });
    };
    self.construct();
}

在所需的模板中,我只需按 id 定位集合,并按 id 添加实例化 FormCollection,例如:

{% extends '::base.html.twig' %}
{% block content %}
{{ form_start(form) }} 
    <h3>Tags</h3>
    <ul id="col-subform1" data-prototype="{{ form_widget(form.subForm1.vars.prototype)|e }}">
        {# iterate over each existing tag and render its only field: name #}
        {% for subForm1 in form.subForm1 %}
            <li>{{ form_row(subForm1) }} </li>
             <ul id="col-subform2" data-prototype="{{ form_widget(subForm2.vars.prototype)|e }}">
            {%for subForm2 in subForm1.subForm2 %}
                <li>{{ form_row(subForm2) }}</li>
            {% endfor %}
        {% endfor %}
    </ul>
{{ form_end(form) }}
<script type="text/javascript" src="/js/form.collection.js"></script>
<script type="text/javascript">
  new FormCollection('col-subform1');
  new FormCollection('col-subform2');
</script>

以防其他人遇到此问题。我遇到了同样的麻烦,我认为这是这种行为的原因。

请随时纠正我,如果我的情况不应该是常规的Symfony行为,并且是由于我的错误。

坦克到控制巴尔斯马,我认为我的问题在原型过程中。

为什么symfony不创建2nd,3rd...集合中的第 n 个子项,是因为第二级集合的原型不像第一级集合那样"空白"。

第一级集合"空白"原型如下所示(对于输入的部分(: someObject_collection___name___ ,其中___name___将被集合索引替换,因此replace(/__name__/g, index)文档的 JavaScript 中。

但是对于第二级集合,原型不是"空白"的,而是使用第一级集合的相关元素索引生成的,例如:someObject_collection_1__otherCollection_1_,而不是我认为someObject_collection_1__otherCollection__name__

因此,当为二级集合调用 replace 函数时,找不到将___name___替换为新子索引的匹配项。

解决方案是更改二级集合的replace调用,将第一级集合的元素索引替换为二级集合的当前元素的索引。

类似于 : newForm.replace(/collection_'d/g, 'collection_' + index); ,表示label标签的for属性和input标签的id属性。

像 : newForm.replace(/'[ligneCPackProds']'['d']/g, '[collection][' + index + ']'); ,表示input标签的name属性。

通过这样做,我能够将我所有的孩子都放在我的二级收藏中。