Skip to content

Commit 3f3155e

Browse files
Merge pull request #44 from oleg-andreyev/allow-to-define-default-data-for-form
added ability to configure default data for forms.
2 parents c304e46 + 6a423d7 commit 3f3155e

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

features/interactive.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,19 @@ Feature: It is possible to interactively fill in a form from the CLI
300300
)
301301
)
302302
"""
303+
304+
Scenario: Command with default form data
305+
When I run the command "form:default_value_command" and I provide as input
306+
| Input |
307+
| foo |
308+
And the output should contain
309+
"""
310+
Street [already save address]:
311+
"""
312+
And the output should contain
313+
"""
314+
Array
315+
(
316+
[street] => foo
317+
)
318+
"""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Matthias\SymfonyConsoleForm\Console\Command;
4+
5+
interface FormBasedCommandWithDefault
6+
{
7+
/**
8+
* Returning default data of a form
9+
*
10+
* @return mixed
11+
*/
12+
public function getFormDefault();
13+
}

src/Console/EventListener/HandleFormBasedCommandEventListener.php

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

55
use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommand;
6+
use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommandWithDefault;
67
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper;
78
use Symfony\Component\Console\Event\ConsoleCommandEvent;
89

@@ -27,8 +28,18 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
2728

2829
$input = $event->getInput();
2930
$output = $event->getOutput();
31+
$defaultData = null;
32+
if ($command instanceof FormBasedCommandWithDefault) {
33+
$defaultData = $command->getFormDefault();
34+
}
3035

31-
$formData = $this->formQuestionHelper->interactUsingForm($command->formType(), $input, $output);
36+
$formData = $this->formQuestionHelper->interactUsingForm(
37+
$command->formType(),
38+
$input,
39+
$output,
40+
[],
41+
$defaultData
42+
);
3243

3344
$command->setFormData($formData);
3445
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Command;
4+
5+
use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommandWithDefault;
6+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
7+
8+
class DefaultFormDataCommand extends PrintFormDataCommand implements FormBasedCommandWithDefault
9+
{
10+
public function getFormDefault()
11+
{
12+
return new Address('already save address');
13+
}
14+
}

test/Form/AddressType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Form\Extension\Core\Type\TextType;
77
use Symfony\Component\Form\FormBuilderInterface;
88
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;
910

1011
class AddressType extends AbstractType
1112
{
@@ -25,7 +26,7 @@ public function configureOptions(OptionsResolver $resolver)
2526
{
2627
$resolver->setDefaults(
2728
[
28-
'data_class' => 'Matthias\SymfonyConsoleForm\Tests\Form\Data\Address',
29+
'data_class' => Address::class,
2930
]
3031
);
3132
}

test/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ services:
2626
tags:
2727
- { name: console.command }
2828

29+
default_value_command:
30+
class: Matthias\SymfonyConsoleForm\Tests\Command\DefaultFormDataCommand
31+
arguments:
32+
- Matthias\SymfonyConsoleForm\Tests\Form\AddressType
33+
- default_value_command
34+
tags:
35+
- { name: console.command }
36+
2937
blocked_addresses_command:
3038
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
3139
arguments:

test/console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Matthias\SymfonyConsoleForm\Tests\AppKernel;
66
use Symfony\Bundle\FrameworkBundle\Console\Application;
7-
use Symfony\Component\Debug\Debug;
7+
use Symfony\Component\ErrorHandler\Debug;
88

99
Debug::enable();
1010

0 commit comments

Comments
 (0)