8000 [Form] Add TranslatableMessage support to choice_label option of Choi… · symfony/symfony@c13a56d · GitHub
[go: up one dir, main page]

Skip to content

Commit c13a56d

Browse files
[Form] Add TranslatableMessage support to choice_label option of ChoiceType
1 parent 3a14868 commit c13a56d

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-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: 16 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,21 @@ function ($object, $key, $value) {
754755
$this->assertFlatViewWithAttr($view);
755756
}
756757

758+
public function testPassTranslatableMessageAsLabelDoesntCastItToString()
759+
{
760+
$view = $this->factory->createView(
761+
$this->list,
762+
[$this->obj1],
763+
static function ($choice, $key, $value) {
764+
return new TranslatableMessage('my_message', ['param1' => 'value1']);
765+
}
766+
);
767+
768+
$this->assertInstanceOf(TranslatableMessage::class, $view->choices[0]->label);
769+
$this->assertEquals('my_message', $view->choices[0]->label->getMessage());
770+
$this->assertArrayHasKey('param1', $view->choices[0]->label->getParameters());
771+
}
772+
757773
private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
758774
{
759775
$this->assertSame(['a', 'b', 'c', 'd'], $list->getValues());

0 commit comments

Comments
 (0)
0