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 ?

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top