Symfony2 精选集

Symfony2 Collection of Choises

我正在制作我的第一个 Symfony 网络应用程序。 现在,在创建了几个表单之后,我发现创建一个包含一系列选择的表单时出现了一些问题。

创建一个 TextTypes 集合非常简单,并且在我的几个表单中工作。现在,当我创建 ChoiseTypes 集合时,它不会在屏幕上显示任何内容。检查 html 代码后,它只显示没有任何选项的 select 语句。此外,原型仅包含 select 而不是选项。

我如何将选择集合添加到表单的位置:

->add('fieldTypes', CollectionType::class, array(
                    'entry_type' => ChoiceType::class,
                    'entry_options' => array(
                        'choices' => $fieldTypes,
                        'required' => true,
                   ),
                     'allow_add' => true,
                    'allow_delete' => true,
                    'prototype' => true,
                ))

这是解决方案的树枝部分:

<td>
                        <ul id="typeTable" data-prototype="{{ form_widget(form.fieldTypes.vars.prototype)|e }}">
                            {% for fieldType in form.fieldTypes %}
                                <li>
                                    {{ form_widget(fieldType) }}
                                </li>
                            {% endfor %}
                        </ul>
                    </td>

以及完整的 JS 代码:

<script type="text/javascript">
        // keep track of how many email fields have been rendered
        var nameCount = '{{ form.fieldNames|length }}';
        var typeCount = '{{ form.fieldTypes|length }}';
        var optionCount = '{{ form.fieldOptions|length }}';

        jQuery(document).ready(function () {
            jQuery('#add-new-field').click(function (e) {
                e.preventDefault();

                var nameList = jQuery('#nameTable');
                var typeList = jQuery('#typeTable');
                var optionList = jQuery('#optionTable');

                // grab the prototype template
                var newNameWidget = nameList.attr('data-prototype');
                var newTypeWidget = typeList.attr('data-prototype');
                var newOptionWidget = optionList.attr('data-prototype');

                //var newTypeWidget = jQuery('#fieldType_prototype');
                // replace the "__name__" used in the id and name of the prototype
                // with a number that's unique to your emails
                // end name attribute looks like name="contact[emails][2]"
                newNameWidget = newNameWidget.replace(/__name__/g, nameCount);
                newTypeWidget = newTypeWidget.replace(/__name__/g, typeCount);
                newOptionWidget = newOptionWidget.replace(/__name__/g, optionCount);

                var $options = $("#fieldType_prototype > option").clone();

                //$(newTypeWidget).append($options);

                $("#form_fieldTypes_" + typeCount).html( $("#fieldType_prototype").html() );

               // newTypeWidget.attr("id","form_fieldTypes_" + typeCount);
               // newTypeWidget.attr('name', 'form[fieldTypes][' + typeCount + ']');
                nameCount++;
                typeCount++;
                optionCount++;

                // create a new list element and add it to the list
                var newLi = jQuery('<li></li>').html(newNameWidget);
                newLi.appendTo(nameList);

                var newLi = jQuery('<li></li>').html(newTypeWidget);
                newLi.appendTo(typeList);

                var newLi = jQuery('<li></li>').html(newOptionWidget);
                newLi.appendTo(optionList);
            });
        })
    </script>

变量 $fieldTypes 包含这个:

array('Text Field' => TextType::class,
          'Text area' => TextareaType::class,
          'Email' => EmailType::class,
          'Number' => IntegerType::class,
          'Url' => UrlType::class,
          'Drop down' => ChoiceType::class,
          'Date' => DateType::class,
          'Checkbox' => CheckboxType::class);      
  }

有人可以帮我解决这个问题吗?

这是一个错误:

xabbuh:

'I guess you might be affected by a regression that will be fixed by github.com/symfony/symfony/pull/17162. Can you test if the changes from that PR solve your problem?'