Skip to content

Commit cd6f185

Browse files
Merge pull request #57 from cristoforocervino/choice-value-option
Allow Symfony form "choice_value" option usage
2 parents fab9b93 + 3b30c12 commit cd6f185

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public function iRunTheCommandAndIProvideAsInput($name, TableNode $input)
5151
$this->runCommandWithInteractiveInput($name, $inputs);
5252
}
5353

54+
/**
55+
* @Given /^I run the command "([^"]*)" and I provide as input "([^"]*)" with parameters$/
56+
*/
57+
public function iRunTheCommandAndIProvideAsInputAndParameters($name, $input, TableNode $parameters)
58+
{
59+
$parameters = $parameters->getHash();
60+
$parameters = array_combine(
61+
array_column($parameters, 'Parameter'),
62+
array_column($parameters, 'Value')
63+
);
64+
65+
$this->runCommandWithInteractiveInputAndParameters($name, [$input], $parameters);
66+
}
67+
5468
/**
5569
* @Given /^I run the command "([^"]*)" and I provide as input "([^"]*)"$/
5670
*/
@@ -70,6 +84,12 @@ public function theOutputShouldBe(PyStringNode $expectedOutput)
7084
);
7185
}
7286

87+
private function runCommandWithInteractiveInputAndParameters($name, array $inputs, array $parameters)
88+
{
89+
$this->tester->setInputs($inputs);
90+
$this->tester->run(\array_merge(['command' => $name], $parameters), array('interactive' => true, 'decorated' => false));
91+
}
92+
7393
private function runCommandWithInteractiveInput($name, array $inputs)
7494
{
7595
$this->tester->setInputs($inputs);

features/interactive.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,30 @@ Feature: It is possible to interactively fill in a form from the CLI
339339
)
340340
"""
341341

342+
Scenario: Choice with object options in interactive mode
343+
Given I run the command "form:unstringable_choices_with_values" and I provide as input "55-rue-du-faubourg-saint-honoré" with parameters
344+
| Parameter | Value |
345+
| --address | 1600-pennsylvania-ave-nw |
346+
Then the command has finished successfully
347+
And the output should contain
348+
"""
349+
Select address [1600-pennsylvania-ave-nw]:
350+
[10-downing-street ] 10 Downing Street
351+
[1600-pennsylvania-ave-nw ] 1600 Pennsylvania Ave NW
352+
[55-rue-du-faubourg-saint-honoré] 55 Rue du Faubourg Saint-Honoré
353+
>
354+
"""
355+
And the output should contain
356+
"""
357+
Array
358+
(
359+
[address] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
360+
(
361+
[street] => 55 Rue du Faubourg Saint-Honoré
362+
)
363+
)
364+
"""
365+
342366
Scenario: Command with default form data
343367
When I run the command "form:default_value_command" and I provide as input
344368
| Input |

features/non-interactive.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ Feature: It is possible to interactively fill in a form from the CLI
5252
"""
5353
There were form errors.
5454
"""
55+
56+
Scenario: Choice with object options in non-interactive mode
57+
When I run a command non-interactively with parameters
58+
| Parameter | Value |
59+
| command | form:unstringable_choices_with_values |
60+
| --address | 1600-pennsylvania-ave-nw |
61+
Then the command has finished successfully
62+
And the output should contain
63+
"""
64+
Array
65+
(
66+
[address] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Address Object
67+
(
68+
[street] => 1600 Pennsylvania Ave NW
69+
)
70+
)
71+
"""

src/Bridge/FormFactory/ConsoleFormWithDefaultValuesAndOptionsFactory.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Matthias\SymfonyConsoleForm\Bridge\FormFactory;
44

55
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Form\Exception\TransformationFailedException;
67
use Symfony\Component\Form\Extension\Core\Type\FormType;
78
use Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension;
89
use Symfony\Component\Form\FormFactoryInterface;
@@ -45,7 +46,19 @@ public function create(string $formType, InputInterface $input, array $options =
4546
continue;
4647
}
4748

48-
$childBuilder->setData($providedValue);
49+
$value = $providedValue;
50+
51+
try {
52+
foreach ($childBuilder->getViewTransformers() as $viewTransformer) {
53+
$value = $viewTransformer->reverseTransform($value);
54+
}
55+
foreach ($childBuilder->getModelTransformers() as $modelTransformer) {
56+
$value = $modelTransformer->reverseTransform($value);
57+
}
58+
} catch (TransformationFailedException) {
59+
}
60+
61+
$childBuilder->setData($value);
4962
}
5063

5164
return $formBuilder->getForm();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 UnstringableChoicesWithValuesType 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_value' => function (Address $address) {
26+
return \strtolower(\str_replace(' ', '-', $address->street));
27+
},
28+
'choice_label' => function (Address $address) {
29+
return $address->street;
30+
},
31+
'data' => new Address('10 Downing Street')
32+
]);
33+
}
34+
}

test/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ services:
138138
tags:
139139
- { name: console.command }
140140

141+
unstringable_choices_with_values_command:
142+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
143+
arguments:
144+
- Matthias\SymfonyConsoleForm\Tests\Form\UnstringableChoicesWithValuesType
145+
- unstringable_choices_with_values
146+
tags:
147+
- { name: console.command }
148+
141149
framework:
142150
form:
143151
csrf_protection: true

0 commit comments

Comments
 (0)