Skip to content

Commit a9605da

Browse files
committed
support ChoiceType data which don't support conversion to string
1 parent e6edf5d commit a9605da

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

features/interactive.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,24 @@ Feature: It is possible to interactively fill in a form from the CLI
223223
[RuntimeException]
224224
Errors out of the form's scope - do you have validation constraints on properties not used in the form? (Violations on unused fields: data.fieldNotUsedInTheForm)
225225
"""
226+
227+
Scenario: Choice with options which cannot be converted to string
228+
When I run the command "form:unstringable_choices" and I provide as input
229+
"""
230+
1[enter]
231+
"""
232+
Then the command has finished successfully
233+
And the output should contain
234+
"""
235+
Select address:
236+
[0] 10 Downing Street
237+
[1] 1600 Pennsylvania Ave NW
238+
[2] 55 Rue du Faubourg Saint-Honoré
239+
> Array
240+
(
241+
[address] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
242+
(
243+
[street] => 1600 Pennsylvania Ave NW
244+
)
245+
)
246+
"""

src/Console/Helper/Question/AlwaysReturnKeyOfChoiceQuestion.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ private function prepareChoices()
118118
foreach ($this->choiceViews as $choiceView) {
119119
$label = $choiceView->label;
120120
if ($choiceView->data != $choiceView->value) {
121-
$label .= ' (<comment>'.$choiceView->data.'</comment>)';
121+
try {
122+
$label .= ' (<comment>' . $choiceView->data . '</comment>)';
123+
} catch (\ErrorException $exception) {
124+
// data cannot be converted to string - do nothing
125+
}
122126
}
123127

124128
$choices[$choiceView->value] = $label;
@@ -136,7 +140,11 @@ private function prepareAutocompleteValues()
136140

137141
foreach ($this->choiceViews as $choiceView) {
138142
$autocompleteValues[] = $choiceView->value;
139-
$autocompleteValues[] = $choiceView->data;
143+
try {
144+
$autocompleteValues[] = (string)$choiceView->data;
145+
} catch (\ErrorException $exception) {
146+
// data cannot be converted to string - do nothing
147+
}
140148
$autocompleteValues[] = $choiceView->label;
141149
}
142150

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
8+
use Symfony\Component\Form\FormBuilderInterface;
9+
10+
/**
11+
* Demonstrates handling of choice data which does not support conversion to string (Address has no __toString())
12+
*/
13+
class UnstringableChoicesType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add('address', ChoiceType::class, [
19+
'label' => 'Select address',
20+
'choices' => [
21+
new Address('10 Downing Street'),
22+
new Address('1600 Pennsylvania Ave NW'),
23+
new Address('55 Rue du Faubourg Saint-Honoré'),
24+
],
25+
'choice_label' => function (Address $address) {
26+
return $address->street;
27+
},
28+
]);
29+
}
30+
}

test/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ services:
9898
tags:
9999
- { name: console.command }
100100

101+
unstringable_choices_command:
102+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
103+
arguments:
104+
- Matthias\SymfonyConsoleForm\Tests\Form\UnstringableChoicesType
105+
- unstringable_choices
106+
tags:
107+
- { name: console.command }
108+
101109
framework:
102110
form:
103111
csrf_protection: true

0 commit comments

Comments
 (0)