Grouping checkboxes in Symfony2

十年热恋 提交于 2019-12-03 13:44:43

问题


It seems that Symfony2 Form component does not handle this common case. Below is what I want in my html

The code looks like :

        ->add('code', 'choice', array(
            'choices' => array(
                'Food'  => array('pizza', 'burger', 'icecream'),
                'Music' => array('poney', 'little', 'pocket'),
            ),
            'multiple' => true,
            'expanded' => true,
            'required' => true
        ));

Which gives in reality the wrong output :

It's wierd because the case with expanded => false is correctly handled

How to handle that case please ?


回答1:


Ok so here's the form_theme solution for this

{% block _user_code_widget %}
    <div {{ block('widget_container_attributes') }}>
        {% for name, choices in form.vars.choices %}
            <ul>
                <li class="checkbox_category">
                    <input type="checkbox" class="checkallsub" /> {{ name }}
                </li>

                {% for key,choice in choices %}
                    <li class="indent">
                        {{ form_widget(form[key]) }}
                        {{ form_label(form[key]) }}
                    </li>
                {% endfor %}
            </ul>
        {% endfor %}
    </div>
{% endblock %}

Of course the extra js layer is missing, but you get the idea.



来源:https://stackoverflow.com/questions/13622586/grouping-checkboxes-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!