Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b414a9

Browse files
committed
[Form] Fix field_value Twig helper
1 parent 494ef42 commit 7b414a9

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public function getFieldName(FormView $view): string
103103
return $view->vars['full_name'];
104104
}
105105

106-
public function getFieldValue(FormView $view): string
106+
/**
107+
* @return string|array
108+
*/
109+
public function getFieldValue(FormView $view)
107110
{
108111
return $view->vars['value'];
109112
}

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionFieldHelpersTest.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ protected function setUp(): void
4949
$this->rawExtension = new FormExtension();
5050
$this->translatorExtension = new FormExtension(new StubTranslator());
5151

52-
$form = $this->factory->createNamedBuilder('register', FormType::class, ['username' => 'tgalopin'])
52+
$data = [
53+
'username' => 'tgalopin',
54+
'choice_multiple' => ['sugar', 'salt'],
55+
];
56+
57+
$form = $this->factory->createNamedBuilder('register', FormType::class, $data)
5358
->add('username', TextType::class, [
5459
'label' => 'base.username',
5560
'label_translation_parameters' => ['%label_brand%' => 'Symfony'],
@@ -77,6 +82,14 @@ protected function setUp(): void
7782
],
7883
'choice_translation_domain' => 'forms',
7984
])
85+
->add('choice_multiple', ChoiceType::class, [
86+
'choices' => [
87+
'base.sugar' => 'sugar',
88+
'base.salt' => 'salt',
89+
],
90+
'multiple' => true,
91+
'expanded' => true,
92+
])
8093
->getForm()
8194
;
8295

@@ -95,6 +108,7 @@ public function testFieldName()
95108
public function testFieldValue()
96109
{
97110
$this->assertSame('tgalopin', $this->rawExtension->getFieldValue($this->view->children['username']));
111+
$this->assertSame(['sugar', 'salt'], $this->rawExtension->getFieldValue($this->view->children['choice_multiple']));
98112
}
99113

100114
public function testFieldLabel()
@@ -234,4 +248,40 @@ public function testFieldTranslatedChoicesGrouped()
234248
$this->assertSame('jp', $choicesArray[1]['choices'][1]['value']);
235249
$this->assertSame('[trans]base.jp[/trans]', $choicesArray[1]['choices'][1]['label']);
236250
}
251+
252+
public function testFieldChoicesMultiple()
253+
{
254+
$choices = $this->rawExtension->getFieldChoices($this->view->children['choice_multiple']);
255+
256+
$choicesArray = [];
257+
foreach ($choices as $label => $value) {
258+
$choicesArray[] = ['label' => $label, 'value' => $value];
259+
}
260+
261+
$this->assertCount(2, $choicesArray);
262+
263+
$this->assertSame('sugar', $choicesArray[0]['value']);
264+
$this->assertSame('base.sugar', $choicesArray[0]['label']);
265+
266+
$this->assertSame('salt', $choicesArray[1]['value']);
267+
$this->assertSame('base.salt', $choicesArray[1]['label']);
268+
}
269+
270+
public function testFieldTranslatedChoicesMultiple()
271+
{
272+
$choices = $this->translatorExtension->getFieldChoices($this->view->children['choice_multiple']);
273+
274+
$choicesArray = [];
275+
foreach ($choices as $label => $value) {
276+
$choicesArray[] = ['label' => $label, 'value' => $value];
277+
}
278+
279+
$this->assertCount(2, $choicesArray);
280+
281+
$this->assertSame('sugar', $choicesArray[0]['value']);
282+
$this->assertSame('[trans]base.sugar[/trans]', $choicesArray[0]['label']);
283+
284+
$this->assertSame('salt', $choicesArray[1]['value']);
285+
$this->assertSame('[trans]base.salt[/trans]', $choicesArray[1]['label']);
286+
}
237287
}

0 commit comments

Comments
 (0)