Skip to content

Commit 83ac068

Browse files
committed
Merge branch 'master' into fix-choicetype-unstringable-value
2 parents 9f2edd6 + 9738346 commit 83ac068

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

features/interactive.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,34 @@ Feature: It is possible to interactively fill in a form from the CLI
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
"""
226226

227+
Scenario: Checkbox field - answer yes
228+
When I run the command "form:coffee" and I provide as input
229+
"""
230+
y[enter]
231+
"""
232+
Then the command has finished successfully
233+
And the output should be
234+
"""
235+
Do you want milk in your coffee?: Array
236+
(
237+
[milk] => 1
238+
)
239+
"""
240+
241+
Scenario: Checkbox field - answer no
242+
When I run the command "form:coffee" and I provide as input
243+
"""
244+
n[enter]
245+
"""
246+
Then the command has finished successfully
247+
And the output should be
248+
"""
249+
Do you want milk in your coffee?: Array
250+
(
251+
[milk] =>
252+
)
253+
"""
254+
227255
Scenario: Choice with options which cannot be converted to string
228256
When I run the command "form:unstringable_choices" and I provide as input
229257
"""

features/non-interactive.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ Feature: It is possible to interactively fill in a form from the CLI
2828
Options:
2929
--password
3030
"""
31+
32+
Scenario: Provide invalid value
33+
When I run the command "form:color --color='invalid color'" non-interactively
34+
Then the command was not successful
35+
And the output should contain
36+
"""
37+
Invalid data provided: color:
38+
ERROR: This value is not valid.
39+
"""
40+
And the output should contain
41+
"""
42+
There were form errors.
43+
"""
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Bridge\Transformer;
4+
5+
use Symfony\Component\Console\Question\ConfirmationQuestion;
6+
use Symfony\Component\Console\Question\Question;
7+
use Symfony\Component\Form\FormInterface;
8+
9+
class CheckboxTransformer extends AbstractTransformer
10+
{
11+
/**
12+
* @param FormInterface $form
13+
*
14+
* @return Question
15+
*/
16+
public function transform(FormInterface $form)
17+
{
18+
return new ConfirmationQuestion($this->questionFrom($form), $this->defaultValueFrom($form));
19+
}
20+
}

src/Bundle/services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ services:
7070
- { name: form_to_question_transformer, form_type: Symfony\Component\Form\Extension\Core\Type\ChoiceType }
7171
- { name: form_to_question_transformer, form_type: Symfony\Component\Form\Extension\Core\Type\CountryType }
7272

73+
matthias_symfony_console_form.checkbox_transformer:
74+
class: Matthias\SymfonyConsoleForm\Bridge\Transformer\CheckboxTransformer
75+
parent: matthias_symfony_console_form.abstract_transformer
76+
public: false
77+
tags:
78+
- { name: form_to_question_transformer, form_type: Symfony\Component\Form\Extension\Core\Type\CheckboxType }
79+
7380
matthias_symfony_console_form.delegating_interactor:
7481
class: Matthias\SymfonyConsoleForm\Bridge\Interaction\DelegatingInteractor
7582
public: false

src/Console/Helper/FormHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ function (FormInterface $formField) use (&$validFormFields) {
9090
},
9191
$form->all()
9292
);
93+
94+
if (!$input->isInteractive()) {
95+
throw new \RuntimeException('There were form errors.');
96+
}
9397
}
9498
} while (!$form->isValid());
9599

test/Form/CoffeeMilkType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
9+
/**
10+
* Demonstrates use of CheckboxType.
11+
*/
12+
class CoffeeMilkType extends AbstractType
13+
{
14+
public function buildForm(FormBuilderInterface $builder, array $options)
15+
{
16+
$builder
17+
->add('milk', CheckboxType::class, [
18+
'label' => 'Do you want milk in your coffee?',
19+
]);
20+
}
21+
}

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+
coffee_command:
102+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
103+
arguments:
104+
- Matthias\SymfonyConsoleForm\Tests\Form\CoffeeMilkType
105+
- coffee
106+
tags:
107+
- { name: console.command }
108+
101109
unstringable_choices_command:
102110
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
103111
arguments:

0 commit comments

Comments
 (0)