-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] choice_attr option in ChoiceType generates options attributes wrong #17019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@nicolas-grekas looks like Btw, be careful that both |
@a-sane my suggestion for a quick resolution is to convert your code to the new way of passing choices for Symfony 2.7+, i.e. with |
The problem seems to be that they keys in ->add('action', 'choice', [
'choices' => [
'a' => 'Yes', // index 0
'b' => 'no', // index 1
'c' => 'Maybe', // index 2
],
'choice_attr' => [
1 => ['data-info' => 'option b'],
],
) <select>
<option value="a">Yes</option>
<option value="b" data-info="option b">no</option>
<option value="c">Maybe</option>
</select> The problem is even more obvious when using: ->add('action', 'choice', [
'choices' => [
'a' => 'Yes', // index 0
'b' => 'no', // index 1
'c' => 'Maybe', // index 2
],
'choice_attr' => function($choice, $key, $value) {
return ['data-choice' => $choice, 'data-key' => $key];
}]
) <select>
<option value="a" data-choice="a" data-key="0">Yes</option>
<option value="b" data-choice="b" data-key="1">no</option>
<option value="c" data-choice="c" data-key="2">Maybe</option>
</select> The keys should be 'a', 'b' and 'c' and not 0, 1 and 2. The callback parameters are named just like they are used in DefaultChoiceListFactory:130. I don't know the difference between |
Hi @ureimers, actually when you set choices like array(
'a' => 'Yes', // $key = 'a', $value = 'Yes' and $index = 0
'b' => 'no', // $key = 'b', $value = 'no' and $index = 1
'c' => 'Maybe', // $key = 'c', $value = 'Maybe' and $index = 2
), because when array(
0 => 'a', // $index => $key value normalized for html attribute "value"
1 => 'b',
2 => 'c',
)
// and
array(
0 => 'Yes', // $index => $choice as value choosen via its label
1 => 'no',
2 => 'Maybe'
) So you got a callable for `choice_attr` => function ($choice, $index, $value) {
return array(
$index => array('data-property' => $choice->getProperty()),
// or
$index => array('data-string-value' => $value)
...
// intial post example is missing the index 0 and that's the real issue.
);
}, Expected behaviour. You can close this issue, feel free to open one in the docs. |
Status: reviewed |
I've updated my answer to better explain what happened in initial post. |
I see, thanks @HeahDude for the reply. The main problem of the initial post seems to be to use the old way of specifying choices ( When using $builder->add('attending', 'choice', array(
'choices' => array(
'Yes' => 2,
'No' => 1,
'Maybe' => 4,
),
'choices_as_values' => true,
'choice_attr' => array(
'Maybe' => array('class' => 'text-muted'),
),
)); But the other way around it doesn't: $builder->add('attending', 'choice', array(
'choices' => array( // switched
2 => 'Yes',
1 => 'No',
4 => 'Maybe',
),
'choices_as_values' => false, // switched
'choice_attr' => array(
4 => array('class' => 'text-muted'), // won't add any class to 'Maybe'
),
)); Internally this may make sense, but from the view of a developer using the form component as an API it doesn't. |
Thanks @ureimers for your feedback, it seems you're right about a different behaviour of Using
|
Phew, that was a comprehensive but very clear roundup. Thank you for taking the time to write it down! |
@ureimers thank you, I was afraid it was to long for someone to read, but I could not make it shorter while keeping it as clear as possible. Hope it will help some others to better understand the way it goes. |
So do you think there is anything we can do in the code or can we "fix" it by adding something to the documentation? What do you think? |
@xabbuh I'm still a beginner/learner, but I don't think the code required to be fixed for that, it seems consistent as it is to me, but yes I think the docs should mention how choices are indexed :
|
Closing for the detailed reasoning by @HeahDude. |
All code tested on clean symfony 2.8 installation
I've created TestFormType
and i got this in view:
same with placeholder option:
i'm not sure, but i think problem near this commit: d94c322
probably in normalizeLegacyChoices method
The text was updated successfully, but these errors were encountered: