6

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!

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.