Question

I'd like to display a dynamic list of checkboxes in a form.

So far, I built a form embedding a static list of checkboxes, and I created a Tag entity for different values in different languages and populated the database. I'd like to replace the static checkboxes by a dynamic list based on the Tag entity.

The documentation says I should use the ChoiceListInterface. But it is really poorly documented. Would you have an example or a global logic explanation to help me ?

Was it helpful?

Solution

You can extend LazyChoiceList abstract class and implement loadChoiceList() method, create a service of it, inject it to the form and set it as choice_list option.

OTHER TIPS

Finally, I used an entity field type :

->add('tags', 'entity', array(
            'class' => 'bndMyBundle:Tag',
            'query_builder' => function(EntityRepository $er){
                return $er->createQueryBuilder('t')
                        ->orderBy('t.en', 'ASC');
            },
            'expanded'   => true,
            'multiple'  => true,
            'property'  => 'en',
        ))

Then, I just need to replace the 'en' value by the user's current locale to choose the right language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top