【问题标题】:Symfony2 collection of forms issueSymfony2 收集表单问题
【发布时间】:2014-07-19 06:47:35
【问题描述】:

我正在尝试创建一个表单集合,其中包含 3 个全局部分和第四部分,其中包含允许复制的 3 个字段。两个表单名称都是“搜索”。我不知道我做错了什么,但在原型中而不是创建整个表单,我得到以下内容:

<input type=&quot;search&quot; id=&quot;search_statistics_collection___name__&quot;     name=&quot;search[statistics_collection][__name__]&quot; required=&quot;required&quot;    class=&quot;form-control&quot; ><a href='#' class='add-statistic btn'>Dodaj statystykę</a>

查看:

{{ form_start(searchForm) }}
{{ form_row(searchForm.league) }}
{{ form_row(searchForm.range) }}
{{ form_row(searchForm.season) }}
<div id="single-proto" data-prototype="{{ form_widget(searchForm.statistics_collection.vars.prototype)|e }}<a href='#' class='add-statistic btn'>Dodaj statystykę</a>"></div>
{% for single in searchForm.statistics_collection %}
    <div class="single-statistic">
        {{ form_label(single.statistic) }}
        {{ form_widget(single.statistic) }}
        {{ form_widget(single.sign) }}
        {{ form_widget(single.value) }}

        <a href="#" class="add-prototype btn">Dodaj statystykę</a>
    </div>
{% endfor %}
{{ form_end(searchForm) }}

表单类:(相关部分)

搜索类型.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('league', 'choice', array(
            'label'=>'Wybierz ligi',
            'choices'=>array(
                'all'=>'Wszystkie ligi',
                'favourite'=>'Moje ulubione ligi',
                'bookie'=>'Ligi mojego bukmachera'
            )
        ))
        ->add('range', 'choice', array(
            'label'=>'Wybierz zakres',
            'choices'=>array(
               'all'=>'Cały mecz - wszystkie mecze',
               'home'=>'Cały mecz - dom',
               'guest'=>'Cały mecz - wyjazd',
               'half_all'=>'Do przerwy - wszystkie mecze',
               'half_home'=>'Do przerwy - dom',
               'half_guest'=>'Do przerwy - wyjazd',
            )
        ))
        ->add('season', 'choice', array(
            'label'=>'Wybierz sezon',
            'choices'=>Statistics::$seasons
        ))
        ->add('statistics_collection', 'collection', array(
            'label'=>'Wybierz statystykę',
            'type' => new SearchSubType(),
            'allow_add'          => true,
            'allow_delete'       => true,
            'prototype'          => true,

        ))
        ->setData(array(
            'statistics_collection' => array(
                array('', '', ''),
            )
        ))
    ;
}

SearchSubType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('statistic', 'choice', array(
            'label'=>'Wybierz statystykę',
            'required'=>false,
            'choices'=>Statistics::$statistics
        ))
        ->add('sign', 'choice', array(
            'label'=>'',
            'required'=>false,
            'choices'=>array(
                'gt'=>'>',
                'lt'=>'<',
                'eq'=>'=',
                'gteq'=>'>=',
                'lteq'=>'<='
            )

        ))
        ->add('value', 'text', array(
            'label'=>'',
            'required'=>false,
        ))
    ;
}

请帮忙:)

解决方案

集合中包含的子表单必须具有不同于父表单的名称。否则一切都搞砸了:)

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    你应该这样做:

        {{ form_start(searchForm) }}
        {{ form_row(searchForm.league) }}
        {{ form_row(searchForm.range) }}
        {{ form_row(searchForm.season) }}
        <ul class="statistics_collection" data-prototype="{{ form_widget(searchForm.statistics_collection.vars.prototype)|e }}"></ul>   
        {{ form_end(searchForm) }}
    

    并且不要忘记像这样在你的树枝文件中暗示 jquery

      {% block javascripts %}
    
          {{ parent() }}
    
          <script type="text/javascript">
            var collectionHolder = $('ul.statistics_collection');
    
            // setup an "add a tag" link
            var $addValueLink = $('<a href="#" class="add_value_link button button-too-small radius">Upload Pictures</a>');
            var $newLinkLi = $('<li></li>').append($addValueLink);
    
            jQuery(document).ready(function() {
                // add the "add a statistics_collection" anchor and li to the tags ul
                collectionHolder.append($newLinkLi);
    
                $addValueLink.on('click', function(e) {
                    addValueForm(collectionHolder, $newLinkLi);
    
                });
            });
    
            function addValueForm(collectionHolder, $newLinkLi) {
                // Get the data-prototype we explained earlier
                var prototype = collectionHolder.attr('data-prototype');
    
                // Replace '__name__' in the prototype's HTML to
                // instead be a number based on the current collection's length.
                var newForm = prototype.replace(/__name__/g, collectionHolder.children().length);
    
                // 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);
            }
    
        </script>
      {% endblock %}
    

    更多详情可以关注How to Embed a Collection of Forms

    【讨论】:

    • 不,不工作。除了删除用于显示当前内容的 for 循环外,您没有更改代码中的任何内容。我仍然在原型中获得单个输入字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多