Reputation: 433
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!
Upvotes: 6
Views: 6955
Reputation: 91
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
Upvotes: 2