Skip to content

[Form] Add missing TranslatableMessage support to choice_label option of ChoiceType #40759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Translation\TranslatableMessage;

/**
* Default implementation of {@link ChoiceListFactoryInterface}.
Expand Down Expand Up @@ -177,7 +178,14 @@ private static function addChoiceView($choice, string $value, $label, array $key
// If "choice_label" is set to false and "expanded" is true, the value false
// should be passed on to the "label" option of the checkboxes/radio buttons
$dynamicLabel = $label($choice, $key, $value);
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;

if (false === $dynamicLabel) {
$label = false;
} elseif ($dynamicLabel instanceof TranslatableMessage) {
$label = $dynamicLabel;
} else {
$label = (string) $dynamicLabel;
}
}

$view = new ChoiceView(
Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Form\ChoiceList\View;

use Symfony\Component\Translation\TranslatableMessage;

/**
* Represents a choice in templates.
*
Expand All @@ -30,10 +32,10 @@ class ChoiceView
/**
* Creates a new choice view.
*
* @param mixed $data The original choice
* @param string $value The view representation of the choice
* @param string|false $label The label displayed to humans; pass false to discard the label
* @param array $attr Additional attributes for the HTML tag
* @param mixed $data The original choice
* @param string $value The view representation of the choice
* @param string|TranslatableMessage|false $label The label displayed to humans; pass false to discard the label
* @param array $attr Additional attributes for the HTML tag
*/
public function __construct($data, string $value, $label, array $attr = [])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Translation\TranslatableMessage;

class DefaultChoiceListFactoryTest extends TestCase
{
Expand Down Expand Up @@ -754,6 +755,24 @@ function ($object, $key, $value) {
$this->assertFlatViewWithAttr($view);
}

/**
* @requires function Symfony\Component\Translation\TranslatableMessage::__construct
*/
public function testPassTranslatableMessageAsLabelDoesntCastItToString()
{
$view = $this->factory->createView(
$this->list,
[$this->obj1],
static function ($choice, $key, $value) {
return new TranslatableMessage('my_message', ['param1' => 'value1']);
}
);

$this->assertInstanceOf(TranslatableMessage::class, $view->choices[0]->label);
$this->assertEquals('my_message', $view->choices[0]->label->getMessage());
$this->assertArrayHasKey('param1', $view->choices[0]->label->getParameters());
}

private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
{
$this->assertSame(['a', 'b', 'c', 'd'], $list->getValues());
Expand Down