Skip to content

Commit a7db37a

Browse files
Merge pull request #55 from matthiasnoback/cristoforocervino-master
fix fatal error when using an object as default value in ChoiceType (continued)
2 parents a9ad35e + 505bc42 commit a7db37a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

features/interactive.feature

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ Feature: It is possible to interactively fill in a form from the CLI
5050

5151
Scenario: Provide an integer
5252
When I run the command "form:age" and I provide as input
53-
| Input |
54-
| 10 |
53+
| Input |
54+
| 10 |
5555
Then the command has finished successfully
5656
And the output should be
5757
"""
@@ -301,6 +301,30 @@ Feature: It is possible to interactively fill in a form from the CLI
301301
)
302302
"""
303303

304+
Scenario: Choice with default value which cannot be converted to string
305+
When I run the command "form:unstringable_choices" and I provide as input
306+
| Input |
307+
| |
308+
Then the command has finished successfully
309+
And the output should contain
310+
"""
311+
Select address:
312+
[0] 10 Downing Street
313+
[1] 1600 Pennsylvania Ave NW
314+
[2] 55 Rue du Faubourg Saint-Honoré
315+
>
316+
"""
317+
And the output should contain
318+
"""
319+
Array
320+
(
321+
[address] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
322+
(
323+
[street] => 10 Downing Street
324+
)
325+
)
326+
"""
327+
304328
Scenario: Command with default form data
305329
When I run the command "form:default_value_command" and I provide as input
306330
| Input |

src/Bridge/Transformer/ChoiceTransformer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Matthias\SymfonyConsoleForm\Console\Helper\Question\AlwaysReturnKeyOfChoiceQuestion;
66
use Symfony\Component\Console\Question\Question;
7+
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
78
use Symfony\Component\Form\FormInterface;
89

910
final class ChoiceTransformer extends AbstractTransformer
@@ -20,4 +21,24 @@ public function transform(FormInterface $form): Question
2021

2122
return $question;
2223
}
24+
25+
protected function defaultValueFrom(FormInterface $form)
26+
{
27+
$defaultValue = parent::defaultValueFrom($form);
28+
29+
/*
30+
* $defaultValue can be anything, since it's the form's (default) data. For the ChoiceType form type the default
31+
* value may be derived from the choice_label option, which transforms the data to a string. We look for the
32+
* choice matching the default data and return its calculated value.
33+
*/
34+
$formView = $form->createView();
35+
foreach ($formView->vars['choices'] as $choiceView) {
36+
/** @var ChoiceView $choiceView */
37+
if ($choiceView->data == $defaultValue) {
38+
return $choiceView->value;
39+
}
40+
}
41+
42+
return $defaultValue;
43+
}
2344
}

test/Form/UnstringableChoicesType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2525
'choice_label' => function (Address $address) {
2626
return $address->street;
2727
},
28+
'data' => new Address('10 Downing Street')
2829
]);
2930
}
3031
}

0 commit comments

Comments
 (0)