Skip to content

[Form] choice_attr option in ChoiceType generates options attributes wrong #17019

Closed
@a-sane

Description

@a-sane

All code tested on clean symfony 2.8 installation

I've created TestFormType

class TestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('testChoice', 'choice', [
                'choices' => [
                    1 => 'test 1',
                    2 => 'test 2',
                    3 => 'test 3',
                ],
                'choice_attr' => [
                    1 => ['data-test' => 'test 1'],
                    2 => ['data-test' => 'test 2'],
                    3 => ['data-test' => 'test 3'],
                ],
            ]);
    }
} 

and i got this in view:

<select id="test_testChoice" name="test[testChoice]">
    <option value="1">test 1</option>
    <option value="2" data-test="test 1">test 2</option>
    <option value="3" data-test="test 2">test 3</option>
</select>

same with placeholder option:

class TestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('testChoice', 'choice', [
                'choices' => [
                    1 => 'test 1',
                    2 => 'test 2',
                    3 => 'test 3',
                ],
                'placeholder' => 'test placeholder',
                'choice_attr' => [
                    1 => ['data-test' => 'test 1'],
                    2 => ['data-test' => 'test 2'],
                    3 => ['data-test' => 'test 3'],
                ],
            ]);
    }
} 
<select id="test_testChoice" name="test[testChoice]" required="required">
    <option value="" selected="selected">test placeholder</option>
    <option value="1">test 1</option>
    <option value="2" data-test="test 1">test 2</option>
    <option value="3" data-test="test 2">test 3</option>
</select> 

i'm not sure, but i think problem near this commit: d94c322
probably in normalizeLegacyChoices method

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