Skip to content

Commit 65cd7c6

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[Form] Add TranslatableMessage support to choice_label option of ChoiceType
1 parent e82ddf2 commit 65cd7c6

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

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(

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
{

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)