You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
Using TranslatableMessage as choice label will result in its __toString() being used to display it, not its trans() representation
How to reproduce
Create a form with a ChoiceType using
'choice_label' => function ($choice, $key, $value) {
return new TranslatableMessage('my_choice', ['%foo%' => $value, '%bar%' => $key], 'form');
},
The rendered page will contain the translation of my_choice, yet without considering the parameters. Reason is that the TranslatableMessage will be casted to string before it can be translated.
Possible Solution
Remove the string typecast in vendor/symfony/form/ChoiceList/Factory/DefaultChoiceListFactory.php:180
…abel option of ChoiceType (alexandre-daubois)
This PR was merged into the 5.2 branch.
Discussion
----------
[Form] Add missing TranslatableMessage support to choice_label option of ChoiceType
It leads to loss of information because it'll use `__toString` to cast, which is incompatible with newly added `TranslatableMessage`, for example.
| Q | A
| ------------- | ---
| Branch? | 5.2 <!-- see below -->
| Bug fix? | yes
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets | Fix#40622 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
| Doc PR | _none_
So this one's a bit tricky in my mind. I didn't want to check if `$dynamicLabel instanceof TranslatableMessage`, because of course it doesn't belong to the same component.
Aside, it would sound so strange to me to add `|object` to `$label` here: https://github.com/symfony/symfony/blob/bb1e1e58aea5318e96d1c22cc8a91668ed7baaaa/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php#L40
But maybe that's the way to go? Requiring your help here. I'm fully open to your ideas, as we're loosing a big feature here by losing `TranslatableMessage` translation parameters.
If the passed object doesn't implement `__toString`, it'll lead to an exception during template rendering, as expected.
Commits
-------
c2873aa [Form] Add TranslatableMessage support to choice_label option of ChoiceType
Symfony version(s) affected: 5.2.*
Description
Using TranslatableMessage as choice label will result in its
__toString()
being used to display it, not itstrans()
representationHow to reproduce
Create a form with a ChoiceType using
The rendered page will contain the translation of
my_choice
, yet without considering the parameters. Reason is that the TranslatableMessage will be casted to string before it can be translated.Possible Solution
Remove the string typecast in vendor/symfony/form/ChoiceList/Factory/DefaultChoiceListFactory.php:180
..or at least if the $dynamicLabel does not implement
Symfony\Contracts\Translation\TranslatableInterface
Additional context
none
The text was updated successfully, but these errors were encountered: