Skip to content

Commit 777b3a9

Browse files
Merge branch '5.2' into 5.x
* 5.2: (23 commits) [Console] Fix Windows code page support [SecurityBundle] Allow ips parameter in access_control accept comma-separated string [Form] Add TranslatableMessage support to choice_label option of ChoiceType Remove code that deals with legacy behavior of PHP_Incomplete_Class [Config][DependencyInjection] Uniformize trailing slash handling [PropertyInfo] Make ReflectionExtractor correctly extract nullability [PropertyInfo] fix attribute namespace with recursive traits [PhpUnitBridge] Fix tests with `@doesNotPerformAssertions` annotations Check redis extension version [Security] Update Russian translations [Notifier] Fix return SentMessage then Messenger not used [VarExporter] Add support of PHP enumerations [Security] Added missing Japanese translations [Security] Added missing Polish translations [Security] Add missing Italian translations #41051 [Security] Missing translations pt_BR getProtocolVersion may return null Fix return type on isAllowedProperty method Make FailoverTransport always pick the first transport [TwigBridge] Fix HTML for translatable custom-file label in Bootstrap 4 theme ...
2 parents 8822890 + 10279a7 commit 777b3a9

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
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}.
@@ -182,7 +183,14 @@ private static function addChoiceView($choice, string $value, $label, array $key
182183
// If "choice_label" is set to false and "expanded" is true, the value false
183184
// should be passed on to the "label" option of the checkboxes/radio buttons
184185
$dynamicLabel = $label($choice, $key, $value);
185-
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
186+
187+
if (false === $dynamicLabel) {
188+
$label = false;
189+
} elseif ($dynamicLabel instanceof TranslatableMessage) {
190+
$label = $dynamicLabel;
191+
} else {
192+
$label = (string) $dynamicLabel;
193+
}
186194
}
187195

188196
$view = new ChoiceView(

ChoiceList/View/ChoiceView.php

Lines changed: 7 additions & 5 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
*
@@ -35,11 +37,11 @@ class ChoiceView
3537
/**
3638
* Creates a new choice view.
3739
*
38-
* @param mixed $data The original choice
39-
* @param string $value The view representation of the choice
40-
* @param string|false $label The label displayed to humans; pass false to discard the label
41-
* @param array $attr Additional attributes for the HTML tag
42-
* @param array $labelTranslationParameters Additional parameters used to translate the label
40+
* @param mixed $data The original choice
41+
* @param string $value The view representation of the choice
42+
* @param string|TranslatableMessage|false $label The label displayed to humans; pass false to discard the label
43+
* @param array $attr Additional attributes for the HTML tag
44+
* @param array $labelTranslationParameters Additional parameters used to translate the label
4345
*/
4446
public function __construct($data, string $value, $label, array $attr = [], array $labelTranslationParameters = [])
4547
{

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
{
@@ -759,6 +760,24 @@ function ($object, $key, $value) {
759760
$this->assertFlatViewWithAttr($view);
760761
}
761762

763+
/**
764+
* @requires function Symfony\Component\Translation\TranslatableMessage::__construct
765+
*/
766+
public function testPassTranslatableMessageAsLabelDoesntCastItToString()
767+
{
768+
$view = $this->factory->createView(
769+
$this->list,
770+
[$this->obj1],
771+
static function ($choice, $key, $value) {
772+
return new TranslatableMessage('my_message', ['param1' => 'value1']);
773+
}
774+
);
775+
776+
$this->assertInstanceOf(TranslatableMessage::class, $view->choices[0]->label);
777+
$this->assertEquals('my_message', $view->choices[0]->label->getMessage());
778+
$this->assertArrayHasKey('param1', $view->choices[0]->label->getParameters());
779+
}
780+
762781
public function testCreateViewFlatLabelTranslationParametersAsArray()
763782
{
764783
$view = $this->factory->createView(

0 commit comments

Comments
 (0)