Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 form collection with checkboxes

I'm building a form with Symfony2 and need to group some checkboxes. I simply cannot figure out how to pass choices/label along to the checkboxes in BonusGroup.

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

Passing data to form:

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

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

Any tips?

Thanks!

like image 704
charliexx Avatar asked Mar 04 '13 11:03

charliexx


1 Answers

First, change form_widget to form_row, but it won't work yet because collection type need a piece of JavaScript to work.

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

like image 92
vjnrv Avatar answered Oct 23 '22 09:10

vjnrv