Skip to content

Commit c0dd5db

Browse files
Merge branch 'master' into dont-cast-form-data-to-array-if-its-not-iterable
2 parents de571ec + 3f3155e commit c0dd5db

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

features/interactive.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,19 @@ Feature: It is possible to interactively fill in a form from the CLI
329329
)
330330
)
331331
"""
332+
333+
Scenario: Command with default form data
334+
When I run the command "form:default_value_command" and I provide as input
335+
| Input |
336+
| foo |
337+
And the output should contain
338+
"""
339+
Street [already save address]:
340+
"""
341+
And the output should contain
342+
"""
343+
Array
344+
(
345+
[street] => foo
346+
)
347+
"""
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/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ services:
3131
arguments:
3232
- Matthias\SymfonyConsoleForm\Tests\Form\AddressesType
3333
- array_collection_form_addresses
34+
35+
default_value_command:
36+
class: Matthias\SymfonyConsoleForm\Tests\Command\DefaultFormDataCommand
37+
arguments:
38+
- Matthias\SymfonyConsoleForm\Tests\Form\AddressType
39+
- default_value_command
3440
tags:
3541
- { name: console.command }
3642

0 commit comments

Comments
 (0)