Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitf378d8f

Browse 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 parent5e609da commitf378d8f

File tree

3 files changed

+77
-10
lines changed

3 files changed

+77
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function loadTypes()
4545
newType\FormType($this->propertyAccessor),
4646
newType\BirthdayType(),
4747
newType\CheckboxType(),
48-
newType\ChoiceType($this->choiceListFactory),
48+
newType\ChoiceType($this->choiceListFactory,$this->translator),
4949
newType\CollectionType(),
5050
newType\CountryType(),
5151
newType\DateIntervalType(),

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ class ChoiceType extends AbstractType
5151
private$choiceListFactory;
5252
private$translator;
5353

54-
/**
55-
* @param TranslatorInterface $translator
56-
*/
57-
publicfunction__construct(ChoiceListFactoryInterface$choiceListFactory =null,$translator =null)
54+
publicfunction__construct(ChoiceListFactoryInterface$choiceListFactory =null, ?TranslatorInterface$translator =null)
5855
{
5956
$this->choiceListFactory =$choiceListFactory ??newCachingFactoryDecorator(
6057
newPropertyAccessDecorator(
6158
newDefaultChoiceListFactory()
6259
)
6360
);
6461

62+
$this->translator =$translator;
63+
6564
// BC, to be removed in 6.0
6665
if ($this->choiceListFactoryinstanceof CachingFactoryDecorator) {
6766
return;
@@ -72,11 +71,6 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
7271
if ($ref->getNumberOfParameters() <3) {
7372
trigger_deprecation('symfony/form','5.1','Not defining a third parameter "callable|null $filter" in "%s::%s()" is deprecated.',$ref->class,$ref->name);
7473
}
75-
76-
if (null !==$translator && !$translatorinstanceof TranslatorInterface) {
77-
thrownew \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;
8074
}
8175

8276
/**
Lines 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+
namespaceSymfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
useSymfony\Component\Form\Extension\Core\CoreExtension;
15+
useSymfony\Component\Form\Test\TypeTestCase;
16+
useSymfony\Contracts\Translation\TranslatorInterface;
17+
18+
class ChoiceTypeTranslationTestextends TypeTestCase
19+
{
20+
publicconstTESTED_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+
protectedfunctiongetExtensions()
31+
{
32+
$translator =$this->createMock(TranslatorInterface::class);
33+
$translator->expects($this->any())->method('trans')
34+
->willReturnCallback(function ($key,$params) {
35+
returnstrtr(sprintf('Translation of: %s',$key),$params);
36+
}
37+
);
38+
39+
returnarray_merge(parent::getExtensions(), [newCoreExtension(null,null,$translator)]);
40+
}
41+
42+
publicfunctiontestInvalidMessageAwarenessForMultiple()
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+
publicfunctiontestInvalidMessageAwarenessForMultipleWithoutScalarOrArrayViewData()
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)

[8]ページ先頭

©2009-2025 Movatter.jp