问题描述:

I follow this documentation: http://symfony.com/doc/current/cookbook/form/form_collections.html.

So, tags displayed as group of textareas, but i need display tags as HTML 'select' element with 'multiple' attribute for saving 'Task'-object including selected 'Tags' later.

Maybe, symfony have a 'black magic' converter from 'tags' entity to form and vice versa. I don't know. But, maybe, i found a wrong way for doing that. Somebody help me please.

网友答案:

If you followed the tutorial, you just need to change your buildForm method on the TagType class, so that the name field becomes a choice type instead of a (guessed) textarea.

Replace :

$builder->add('name');

By :

$builder->add('name', 'choice', array(
    'choices' => array(
       'valueA' => 'labelA',
       'valueB' => 'labelB',
       'valueC' => 'labelC',
       'valueD' => 'labelD',
       // ...
    ),
    'multiple' => true,
);

And if your choice list is dynamic, a good practice is to declare your form type as a service.

Some more info about the choice field type are documented here.

相关阅读:
Top