Skip to content

Commit 505bc42

Browse files
Add example, simplify loop
1 parent 43faada commit 505bc42

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ protected function defaultValueFrom(FormInterface $form)
2626
{
2727
$defaultValue = parent::defaultValueFrom($form);
2828

29-
// $defaultValue could be an object
30-
// Let's find out what form computed to be its view representation
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+
*/
3134
$formView = $form->createView();
32-
$defaultValueFromChoiceView = \array_reduce(
33-
$formView->vars['choices'],
34-
static fn (
35-
?string $carry,
36-
ChoiceView $choiceView
37-
): ?string => ($carry === null && $choiceView->data === $defaultValue) ? $choiceView->value : $carry
38-
);
39-
return $defaultValueFromChoiceView ?? $defaultValue;
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;
4043
}
4144
}

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)