3

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 ?

Yako
  • 3,405
  • 9
  • 41
  • 71

2 Answers2

2

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.

Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
  • Thanks, but this looks difficult to implement ; I didn't find any examples of this on Google :( – Yako Nov 01 '12 at 13:36
  • 2
    @Yako It's pretty easy. Like this: http://stackoverflow.com/questions/13313415/symfony2-populate-choice-list-from-api-data :) – Tek Jul 09 '14 at 21:30
1

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.

Yako
  • 3,405
  • 9
  • 41
  • 71
  • 1
    Hey Yako, this answer does not address the question at all - it should not be marked as accepted. The one from @m2mdas addresses the question. His answer is maybe too short, but it's strictly relevant to the question. I think you should unaccept it and either accept m2mdas's answer or do not accept any and wait for a better explanation. (Just my side note with good intention) – Taz Sep 01 '14 at 12:19