Search code examples
symfonycollections

Symfony2 Collection of Choises


I'm making my first Symfony web app. Now after creating a couple of forms I found some problem creating a form with a collection of choises.

Creating a collection of TextTypes is very easy and is working in a couple of my forms. Now when I create a collections of ChoiseTypes it does not show anything on the screen. After checking the html code it only show the select statement without any options. Also the prototype contains only the select and not the options.

Where is how I add the collections of choises to the form:

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

And this is the twig part of the sollution:

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

And the full JS code:

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

Variable $fieldTypes contains this:

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

Can someone help me with this issue?


Solution

  • It was a bug:

    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?'