Skip to content

Commit 9738346

Browse files
Merge pull request #38 from igneus/checkbox_type_transformer
Add transformer for CheckboxType
2 parents f87325d + f584b05 commit 9738346

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

features/interactive.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,31 @@ 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: 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+
"""
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

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
framework:
102110
form:
103111
csrf_protection: true

0 commit comments

Comments
 (0)