Skip to content

[Form] Fix ChoiceType to effectively set and use translator #41534

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
merged 1 commit into from
Oct 10, 2021
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 @@ -45,7 +45,7 @@ protected function loadTypes()
new Type\FormType($this->propertyAccessor),
new Type\BirthdayType(),
new Type\CheckboxType(),
new Type\ChoiceType($this->choiceListFactory),
new Type\ChoiceType($this->choiceListFactory, $this->translator),
new Type\CollectionType(),
new Type\CountryType(),
new Type\DateIntervalType(),
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
)
);

if (null !== $translator && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be han instance of "%s", "%s" given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->translator = $translator;

// BC, to be removed in 6.0
if ($this->choiceListFactory instanceof CachingFactoryDecorator) {
return;
Expand All @@ -72,11 +77,6 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
if ($ref->getNumberOfParameters() < 3) {
trigger_deprecation('symfony/form', '5.1', 'Not defining a third parameter "callable|null $filter" in "%s::%s()" is deprecated.', $ref->class, $ref->name);
}

if (null !== $translator && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be han instance of "%s", "%s" given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->translator = $translator;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Contracts\Translation\TranslatorInterface;

class ChoiceTypeTranslationTest extends TypeTestCase
{
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';

private $choices = [
'Bernhard' => 'a',
'Fabien' => 'b',
'Kris' => 'c',
'Jon' => 'd',
'Roman' => 'e',
];

protected function getExtensions()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->any())->method('trans')
->willReturnCallback(function ($key, $params) {
return strtr(sprintf('Translation of: %s', $key), $params);
}
);

return array_merge(parent::getExtensions(), [new CoreExtension(null, null, $translator)]);
}

public function testInvalidMessageAwarenessForMultiple()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
'invalid_message' => 'You are not able to use value "{{ value }}"',
]);

$form->submit(['My invalid choice']);
$this->assertEquals(
"ERROR: Translation of: You are not able to use value \"My invalid choice\"\n",
(string) $form->getErrors(true)
);
}

public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
'invalid_message' => 'You are not able to use value "{{ value }}"',
]);

$form->submit(new \stdClass());
$this->assertEquals(
"ERROR: Translation of: You are not able to use value \"stdClass\"\n",
(string) $form->getErrors(true)
);
}
}