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

Skip to content
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
[Form] Fix field_value Twig helper
  • Loading branch information
tgalopin committed Oct 7, 2020
commit 7b414a96df3c394745d37d0105db1e72bedad33c
5 changes: 4 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function getFieldName(FormView $view): string
return $view->vars['full_name'];
}

public function getFieldValue(FormView $view): string
/**
* @return string|array
*/
public function getFieldValue(FormView $view)
{
return $view->vars['value'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ protected function setUp(): void
$this->rawExtension = new FormExtension();
$this->translatorExtension = new FormExtension(new StubTranslator());

$form = $this->factory->createNamedBuilder('register', FormType::class, ['username' => 'tgalopin'])
$data = [
'username' => 'tgalopin',
'choice_multiple' => ['sugar', 'salt'],
];

$form = $this->factory->createNamedBuilder('register', FormType::class, $data)
->add('username', TextType::class, [
'label' => 'base.username',
'label_translation_parameters' => ['%label_brand%' => 'Symfony'],
Expand Down Expand Up @@ -77,6 +82,14 @@ protected function setUp(): void
],
'choice_translation_domain' => 'forms',
])
->add('choice_multiple', ChoiceType::class, [
'choices' => [
'base.sugar' => 'sugar',
'base.salt' => 'salt',
],
'multiple' => true,
'expanded' => true,
])
->getForm()
;

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

public function testFieldLabel()
Expand Down Expand Up @@ -234,4 +248,40 @@ public function testFieldTranslatedChoicesGrouped()
$this->assertSame('jp', $choicesArray[1]['choices'][1]['value']);
$this->assertSame('[trans]base.jp[/trans]', $choicesArray[1]['choices'][1]['label']);
}

public function testFieldChoicesMultiple()
{
$choices = $this->rawExtension->getFieldChoices($this->view->children['choice_multiple']);

$choicesArray = [];
foreach ($choices as $label => $value) {
$choicesArray[] = ['label' => $label, 'value' => $value];
}

$this->assertCount(2, $choicesArray);

$this->assertSame('sugar', $choicesArray[0]['value']);
$this->assertSame('base.sugar', $choicesArray[0]['label']);

$this->assertSame('salt', $choicesArray[1]['value']);
$this->assertSame('base.salt', $choicesArray[1]['label']);
}

public function testFieldTranslatedChoicesMultiple()
{
$choices = $this->translatorExtension->getFieldChoices($this->view->children['choice_multiple']);

$choicesArray = [];
foreach ($choices as $label => $value) {
$choicesArray[] = ['label' => $label, 'value' => $value];
}

$this->assertCount(2, $choicesArray);

$this->assertSame('sugar', $choicesArray[0]['value']);
$this->assertSame('[trans]base.sugar[/trans]', $choicesArray[0]['label']);

$this->assertSame('salt', $choicesArray[1]['value']);
$this->assertSame('[trans]base.salt[/trans]', $choicesArray[1]['label']);
}
}