2

currently i have a form, that generates a Drop-Down-Select from my category-entity:

        $builder
            ->add('category', 'entity',
                    array('class' => 'TrackerMembersBundle:Category',
                            'property' => 'title',));

Now i want to generate checkboxes instead, where i can select more than one option...i searched the symfony2 documentation, but could not find an easy way to do it directly from my Entity.Any idea?

3 Answers 3

11

Adding 'multiple' => true to the option array (the last one where class and property are) gives you a multi-select.

Then you could override the choice_widget block, using form theming.

Something like this might work:

{% block choice_widget %}
{% spaceless %}
    {% for choice, label in choices %}
        <label>
            <input type="checkbox" value="{{ choice }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>
            {{ label|trans }}
        </label>
    {% endfor %}
{% endspaceless %}
{% endblock choice_widget %}
0
10

Because EntityType has parent the ChoiceType, you can use any option from the choice type.

In your case, configuring your category field as following would render checkboxes:

$builder
    ->add('category', 'entity', array(
        'class' => 'TrackerMembersBundle:Category',
        'property' => 'title',
        'multiple' => true,
        'expanded' => true,
    )
);

Note the usage of multiple AND expanded in the options.

2
  • the multiple and expanded did not have any effect on the dropdown.
    – Confidence
    Feb 28, 2012 at 8:23
  • That's not normal then :) I tested in my own project before answering you, and it worked well. Do you have errors messages or something ? Feb 28, 2012 at 10:06
0

Try

       $builder
        ->add('category', 'checkbox',
                array('class' => 'TrackerMembersBundle:Category',
                        'property' => 'title',));#

http://symfony.com/doc/current/reference/forms/types/checkbox.html

2
  • i tried this before , i get: The options "class", "property", "multiple" do not exist
    – Confidence
    Feb 27, 2012 at 14:14
  • This won't work. He still needs the entity formType, not the checkbox type. Feb 27, 2012 at 22:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.