Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 00dc970

Browse filesBrowse files
[Form] Fix ChoiceType Extension to effectively set and use the translator
ChoiceType constructor line order changed to set translator before early return; CoreExtension injects translator to ChoiceType
1 parent 5e609da commit 00dc970
Copy full SHA for 00dc970

File tree

3 files changed

+79
-6
lines changed
Filter options

3 files changed

+79
-6
lines changed

‎src/Symfony/Component/Form/Extension/Core/CoreExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/CoreExtension.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function loadTypes()
4545
new Type\FormType($this->propertyAccessor),
4646
new Type\BirthdayType(),
4747
new Type\CheckboxType(),
48-
new Type\ChoiceType($this->choiceListFactory),
48+
new Type\ChoiceType($this->choiceListFactory, $this->translator),
4949
new Type\CollectionType(),
5050
new Type\CountryType(),
5151
new Type\DateIntervalType(),

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
6262
)
6363
);
6464

65+
if (null !== $translator && !$translator instanceof TranslatorInterface) {
66+
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)));
67+
}
68+
$this->translator = $translator;
69+
6570
// BC, to be removed in 6.0
6671
if ($this->choiceListFactory instanceof CachingFactoryDecorator) {
6772
return;
@@ -72,11 +77,6 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
7277
if ($ref->getNumberOfParameters() < 3) {
7378
trigger_deprecation('symfony/form', '5.1', 'Not defining a third parameter "callable|null $filter" in "%s::%s()" is deprecated.', $ref->class, $ref->name);
7479
}
75-
76-
if (null !== $translator && !$translator instanceof TranslatorInterface) {
77-
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)));
78-
}
79-
$this->translator = $translator;
8080
}
8181

8282
/**
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
use Symfony\Component\Form\Extension\Core\CoreExtension;
15+
use Symfony\Component\Form\Test\TypeTestCase;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
17+
18+
class ChoiceTypeTranslationTest extends TypeTestCase
19+
{
20+
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
21+
22+
private $choices = [
23+
'Bernhard' => 'a',
24+
'Fabien' => 'b',
25+
'Kris' => 'c',
26+
'Jon' => 'd',
27+
'Roman' => 'e',
28+
];
29+
30+
protected function getExtensions()
31+
{
32+
$translator = $this->createMock(TranslatorInterface::class);
33+
$translator->expects($this->any())->method('trans')
34+
->willReturnCallback(function ($key, $params) {
35+
return strtr(sprintf('Translation of: %s', $key), $params);
36+
}
37+
);
38+
39+
return array_merge(parent::getExtensions(), [new CoreExtension(null, null, $translator)]);
40+
}
41+
42+
public function testInvalidMessageAwarenessForMultiple()
43+
{
44+
$form = $this->factory->create(static::TESTED_TYPE, null, [
45+
'multiple' => true,
46+
'expanded' => false,
47+
'choices' => $this->choices,
48+
'invalid_message' => 'You are not able to use value "{{ value }}"',
49+
]);
50+
51+
$form->submit(['My invalid choice']);
52+
$this->assertEquals(
53+
"ERROR: Translation of: You are not able to use value \"My invalid choice\"\n",
54+
(string) $form->getErrors(true)
55+
);
56+
}
57+
58+
public function testInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
59+
{
60+
$form = $this->factory->create(static::TESTED_TYPE, null, [
61+
'multiple' => true,
62+
'expanded' => false,
63+
'choices' => $this->choices,
64+
'invalid_message' => 'You are not able to use value "{{ value }}"',
65+
]);
66+
67+
$form->submit(new \stdClass());
68+
$this->assertEquals(
69+
"ERROR: Translation of: You are not able to use value \"stdClass\"\n",
70+
(string) $form->getErrors(true)
71+
);
72+
}
73+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.