Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping checkboxes in Symfony2

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

enter image description here

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 :

enter image description here

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

enter image description here

How to handle that case please ?

like image 202
sf_tristanb Avatar asked Nov 29 '12 09:11

sf_tristanb


1 Answers

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.

like image 160
sf_tristanb Avatar answered Nov 14 '22 21:11

sf_tristanb