-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Symfony not selecting choice based on default choice data #17939
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
Hi @MichaelMackus, there were many Some pieces of your form type implementation might help if so. |
Thanks for the update. Could you please provide a sample of your builder config ? Especially the parts where you define choices, and default data. |
All the code is in my repo: |
@MichaelMackus Thanks for reporting this issue and special thanks for the detailed report! What you experience is not a bug: Symfony compares the choices in the Second, Symfony will never automatically cast your objects to strings in order to generate the values. That's because Symfony doesn't know whether your object can be cast to a string or not. The solution is to set the $resolver->setDefaults([
'data_class' => MyValueObject::class,
'choices' => $this->getChoices(),
'choices_as_values' => true,
'choice_value' => function (MyValueObject $object) {
// Use the string representation as values
return (string) $object;
},
]); The good thing about |
This sentence should definitely be in the docs somewhere (I didn't find it at least) - I spent ages trying to figure this out! 👍 |
Issue
Symfony v2.8.3 is not selecting the default choice based on data set via the "data" parameter, when the choices consist of value objects. Its also not properly populating the "value" of the choices.
Steps to reproduce
Note: this is just a symfony project created with
symfony new acme 2.8
Results
When navigating to http://127.0.0.1:8000
Expected Results
"object 1" should be selected, but its not. Also, the values should be the string representation of the value objects (e.g. "object 1", "object 2", etc.), but its a simple numerical index.
Take a look at MyValueObject and MyChoiceType in particular. Composer is locked to symfony 2.8.3
Form submission works fine, and the resulting object gets returned properly from the form. Its just default data that seems broken.
Workaround
Workaround is to add this to the choice default options:
But annoying, and the docs report this as fixed in 2.8
The text was updated successfully, but these errors were encountered: