Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffbf8c2

Browse files
bug #40759 [Form] Add missing TranslatableMessage support to choice_label 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
2 parents 2d719ac + c2873aa commit ffbf8c2

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
2121
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
2222
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
23+
use Symfony\Component\Translation\TranslatableMessage;
2324

2425
/**
2526
* Default implementation of {@link ChoiceListFactoryInterface}.
@@ -177,7 +178,14 @@ private static function addChoiceView($choice, string $value, $label, array $key
177178
// If "choice_label" is set to false and "expanded" is true, the value false
178179
// should be passed on to the "label" option of the checkboxes/radio buttons
179180
$dynamicLabel = $label($choice, $key, $value);
180-
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
181+
182+
if (false === $dynamicLabel) {
183+
$label = false;
184+
} elseif ($dynamicLabel instanceof TranslatableMessage) {
185+
$label = $dynamicLabel;
186+
} else {
187+
$label = (string) $dynamicLabel;
188+
}
181189
}
182190

183191
$view = new ChoiceView(

src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Form\ChoiceList\View;
1313

14+
use Symfony\Component\Translation\TranslatableMessage;
15+
1416
/**
1517
* Represents a choice in templates.
1618
*
@@ -30,10 +32,10 @@ class ChoiceView
3032
/**
3133
* Creates a new choice view.
3234
*
33-
* @param mixed $data The original choice
34-
* @param string $value The view representation of the choice
35-
* @param string|false $label The label displayed to humans; pass false to discard the label
36-
* @param array $attr Additional attributes for the HTML tag
35+
* @param mixed $data The original choice
36+
* @param string $value The view representation of the choice
37+
* @param string|TranslatableMessage|false $label The label displayed to humans; pass false to discard the label
38+
* @param array $attr Additional attributes for the HTML tag
3739
*/
3840
public function __construct($data, string $value, $label, array $attr = [])
3941
{

src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
2222
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
2323
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
24+
use Symfony\Component\Translation\TranslatableMessage;
2425

2526
class DefaultChoiceListFactoryTest extends TestCase
2627
{
@@ -754,6 +755,24 @@ function ($object, $key, $value) {
754755
$this->assertFlatViewWithAttr($view);
755756
}
756757

758+
/**
759+
* @requires function Symfony\Component\Translation\TranslatableMessage::__construct
760+
*/
761+
public function testPassTranslatableMessageAsLabelDoesntCastItToString()
762+
{
763+
$view = $this->factory->createView(
764+
$this->list,
765+
[$this->obj1],
766+
static function ($choice, $key, $value) {
767+
return new TranslatableMessage('my_message', ['param1' => 'value1']);
768+
}
769+
);
770+
771+
$this->assertInstanceOf(TranslatableMessage::class, $view->choices[0]->label);
772+
$this->assertEquals('my_message', $view->choices[0]->label->getMessage());
773+
$this->assertArrayHasKey('param1', $view->choices[0]->label->getParameters());
774+
}
775+
757776
private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
758777
{
759778
$this->assertSame(['a', 'b', 'c', 'd'], $list->getValues());

0 commit comments

Comments
 (0)