To set HTML attributes for elements, use the choice_attr option:
Since the version of the framework is not specified by you, but by the type of the arguments passed it is clear that it is below 2.8, examples will be given for symfony 2.7. For use in symfony versions> = 2.8, you must use the full class name (FQCN) ChoiceType::class instead of choice .
The value of choice_attr can be of the following types:
1. array
Suitable if the attributes of the elements do not differ.
$builder->add('field', 'choice', [ // ... 'choice_attr' => [ 'data-option' => 'awesome-value', ], ]);
2. callable
Allows the most flexible management of attributes.
$builder->add('field', 'choice', [ // ... 'choice_attr' => function ($val, $key, $index) { return ['data-option' => 'attending_' . $key]; }, ]);
It can be used if the elements are objects, and the attribute value is one of the object properties:
$builder->add('field', 'choice', [ // ... 'choices' => [ new Person('John'), new Person('Alice'), new Person('Michael'), ], 'choice_attr' => 'name', ]);
UPDATED: symfony 2.6
In this case, the choice_attr will not work, since it appeared in version 2.7. Moreover, there are several options for solving this problem:
For clarity, I will give an example of the implementation of the first option. I will limit myself to the ability to pass only an array of attributes as a value.
Create a class extending choice type:
<?php // AppBundle/Form/Extension/ChoiceTypeExtension.php namespace AppBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class ChoiceTypeExtension extends AbstractTypeExtension { public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['choice_attr'] = $options['choice_attr']; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults([ 'choice_attr' => [], ]); } public function getExtendedType() { return 'choice'; } }
Register it in the container and assign the form.type_extension tag
# app/config/service.yml services: app.choice_type_extension: class: AppBundle\Form\Extension\ChoiceTypeExtension tags: - { name: form.type_extension, alias: choice }
Overload the standard choice_widget_options template:
{# app/Resources/views/form_theme.html.twig #} {%- block choice_widget_options -%} {%- set attr = choice_attr -%} {% for group_label, choice in options %} {%- if choice is iterable -%} <optgroup label="{{ group_label|trans({}, translation_domain) }}"> {% set options = choice %} {{- block('choice_widget_options') -}} </optgroup> {%- else -%} <option value="{{ choice.value }}"{% if attr %} {{ block('attributes') }}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ translation_domain is same as(false) ? choice.label : choice.label|trans({}, translation_domain) }}</option> {%- endif -%} {% endfor %} {%- endblock -%} {% block attributes -%} {%- for attrname, attrvalue in attr -%} {{- " " -}} {%- if attrname in ['placeholder', 'title'] -%} {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {%- elseif attrvalue is same as(true) -%} {{- attrname }}="{{ attrname }}" {%- elseif attrvalue is not same as(false) -%} {{- attrname }}="{{ attrvalue }}" {%- endif -%} {%- endfor -%} {%- endblock attributes -%}
Add the template created in the previous step to the configuration:
# app/config/config.yml twig: form_themes: - "form_theme.html.twig"
We use:
$builder->add('gender', 'choice', [ 'required' => false, 'choices' => [ 'm' => 'Male', 'f' => 'Female', ], 'choice_attr' => [ 'data-option' => 'awesome' ], ]);
Code checked for symfony version 2.6.0