Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions features/interactive.feature
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,19 @@ Feature: It is possible to interactively fill in a form from the CLI
)
)
"""

Scenario: Command with default form data
When I run the command "form:default_value_command" and I provide as input
| Input |
| foo |
And the output should contain
"""
Street [already save address]:
"""
And the output should contain
"""
Array
(
[street] => foo
)
"""
13 changes: 13 additions & 0 deletions src/Console/Command/FormBasedCommandWithDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Matthias\SymfonyConsoleForm\Console\Command;

interface FormBasedCommandWithDefault
{
/**
* Returning default data of a form
*
* @return mixed
*/
public function getFormDefault();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Matthias\SymfonyConsoleForm\Console\EventListener;

use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommand;
use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommandWithDefault;
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

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

$input = $event->getInput();
$output = $event->getOutput();
$defaultData = null;
if ($command instanceof FormBasedCommandWithDefault) {
$defaultData = $command->getFormDefault();
}

$formData = $this->formQuestionHelper->interactUsingForm($command->formType(), $input, $output);
$formData = $this->formQuestionHelper->interactUsingForm(
$command->formType(),
$input,
$output,
[],
$defaultData
);

$command->setFormData($formData);
}
Expand Down
14 changes: 14 additions & 0 deletions test/Command/DefaultFormDataCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace Matthias\SymfonyConsoleForm\Tests\Command;

use Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommandWithDefault;
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;

class DefaultFormDataCommand extends PrintFormDataCommand implements FormBasedCommandWithDefault
{
public function getFormDefault()
{
return new Address('already save address');
}
}
3 changes: 2 additions & 1 deletion test/Form/AddressType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Matthias\SymfonyConsoleForm\Tests\Form\Data\Address;

class AddressType extends AbstractType
{
Expand All @@ -25,7 +26,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => 'Matthias\SymfonyConsoleForm\Tests\Form\Data\Address',
'data_class' => Address::class,
]
);
}
Expand Down
8 changes: 8 additions & 0 deletions test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ services:
tags:
- { name: console.command }

default_value_command:
class: Matthias\SymfonyConsoleForm\Tests\Command\DefaultFormDataCommand
arguments:
- Matthias\SymfonyConsoleForm\Tests\Form\AddressType
- default_value_command
tags:
- { name: console.command }

blocked_addresses_command:
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
arguments:
Expand Down
2 changes: 1 addition & 1 deletion test/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Matthias\SymfonyConsoleForm\Tests\AppKernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Debug\Debug;
use Symfony\Component\ErrorHandler\Debug;

Debug::enable();

Expand Down