I
I
Ivan Antonov2015-08-03 14:01:43
symfony
Ivan Antonov, 2015-08-03 14:01:43

How to add data-type to option in select using Symfony form builder?

I'm creating a form using Symfony's built-in form builder.
$builder->add('category', 'select');
category is a reference to an entity, this entity, in addition to id and name, also has a type. How can I add this line so that when creating a select
{{ form_widget(form.category) }}
, the data-type attribute with the value from the type field is added to the option.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Antonov, 2015-08-04
@antonowano

$builder->add('category', null, [
    'choice_attr' => function ($entity, $currentChoiceKey) {
        return ['data-type' => $entity->getType()];
    }
]);

Source

B
Budanov Evgeniy, 2015-08-03
@zk-zeka

something like this
zeka.kiev.ua/symfony2-form-%D0%B4%D0%BE%D0%B1%D0%B...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question