Skip to content

[Form] Choice attr is applied to each <option /> #19016

Closed
@alanhartless

Description

@alanhartless

Just upgraded to Symfony 2.7 from 2.5 and noticed that choice field attributes are getting applied to each options in PHP form templates. When I remove the call to the attributes block in the following line, the option attributes are not generated.

https://github.com/symfony/framework-bundle/blob/master/Resources/views/Form/choice_widget_options.html.php#L11

For example,

            $builder->add(
                'source',
                'choice',
                [
                    'choices'     => $choices,
                    'expanded'    => false,
                    'multiple'    => false,
                    'label'       => 'do_something,
                    'label_attr'  => ['class' => 'control-label'],
                    'empty_value' => false,
                    'required'    => false,
                    'attr'        => [
                        'class'    => 'form-control',
                        'onchange' => 'doSomething();'
                    ]
                ]
            );

Will result in something like

<select class='form-control' onchange='doSomething();'>
    <option class='form-control' onchange='doSomething();' value=''>Foo</option>
    <option class='form-control' onchange='doSomething();' value=''>Bar</option>
</select>

Is this intentional and if so, is there a recommended way to not do this (even if I set choice_attr => [], it seems to be populating the option attributes with the select's attributes). Or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions