Symfony2 checkbox forms collection

I am creating a form with Symfony2 and you need to group some checkboxes. I just can't figure out how to pass the selection / label along with the checkboxes to BonusGroup.

the form:

$builder->add('groups', 'collection', array(
     'type' => new BonusGroup(),
     'allow_add' => false,
     'allow_delete' => false,
     'by_reference' => false
));

BonusGroup ():

$builder->add('bonus', 'choice', array(
      'choices'  => $options['bonus'],
      'multiple' => true,
      'expanded' => true
));

View.twig:

{% for group in form.groups %}
     {{ form_label(group) }}
     {% for final in group.bonus %}
          {{ form_widget(final) }}
     {% endfor %}
{% endfor %}

Form data submission:

$data = array(
    'groups' =>
        array ('Group 1 label' => array())
);

$form = $app['form.factory']->createBuilder(new Form(), $data))->getForm();

Any tips?

Thank!

+5
source share
1 answer

First change form_widget to form_row , but it won’t work yet, because the collection type requires some JavaScript to work.

See examples here: http://symfony.com/doc/2.1/reference/forms/types/collection.html

+2
source

All Articles