Skip to content

Commit 1842632

Browse files
author
Cristoforo Cervino
committed
allow to use symfony form help option
1 parent a7db37a commit 1842632

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

features/interactive.feature

+14
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ Feature: It is possible to interactively fill in a form from the CLI
169169
)
170170
"""
171171

172+
Scenario: Help message
173+
When I run the command "form:help_message" and I provide as input
174+
| Input |
175+
| empty |
176+
Then the command has finished successfully
177+
And the output should be
178+
"""
179+
Restricted to be ACME Inc.
180+
Company: Array
181+
(
182+
[fieldCompany] => empty
183+
)
184+
"""
185+
172186
Scenario: Translatable label
173187
When I run the command "form:translatable_label" and I provide as input
174188
| Input |

src/Bridge/Transformer/AbstractTransformer.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ public function __construct(TranslatorInterface $translator)
2424

2525
protected function questionFrom(FormInterface $form): string
2626
{
27-
$question = $this->translator->trans(FormUtil::label($form), [], $this->translationDomainFrom($form));
27+
$translationDomain = $this->translationDomainFrom($form);
28+
$question = $this->translator->trans(FormUtil::label($form), [], $translationDomain);
29+
$help = FormUtil::help($form);
30+
if ($help !== null) {
31+
$help = $this->translator->trans($help, FormUtil::helpTranslationParameters($form), $translationDomain);
32+
}
2833

29-
return $this->formattedQuestion($question, $this->defaultValueFrom($form));
34+
return $this->formattedQuestion($question, $this->defaultValueFrom($form), $help);
3035
}
3136

3237
/**
@@ -55,8 +60,8 @@ protected function translationDomainFrom(FormInterface $form): ?string
5560
* @param mixed $defaultValue
5661
* @return string
5762
*/
58-
protected function formattedQuestion(string $question, $defaultValue): string
63+
protected function formattedQuestion(string $question, $defaultValue, ?string $help = null): string
5964
{
60-
return Format::forQuestion($question, $defaultValue);
65+
return Format::forQuestion($question, $defaultValue, $help);
6166
}
6267
}

src/Console/Formatter/Format.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ final class Format
77
/**
88
* @param mixed $defaultValue
99
*/
10-
public static function forQuestion(string $question, $defaultValue): string
10+
public static function forQuestion(string $question, $defaultValue, ?string $help = null): string
1111
{
1212
$default = $defaultValue ? strtr(
1313
' [{defaultValue}]',
1414
['{defaultValue}' => (string)$defaultValue]
1515
) : '';
16-
16+
$help = $help !== null ? '<comment>' . $help . '</comment>' . "\n" : '';
1717
return strtr(
18-
'<question>{question}</question>{default}: ',
18+
'{help}<question>{question}</question>{default}: ',
1919
[
20+
'{help}' => $help,
2021
'{question}' => $question,
2122
'{default}' => (string)$default,
2223
]

src/Form/FormUtil.php

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public static function label(FormInterface $form): string
4747
return $label;
4848
}
4949

50+
public static function help(FormInterface $form): ?string
51+
{
52+
return $form->getConfig()->getOption('help');
53+
}
54+
55+
public static function helpTranslationParameters(FormInterface $form): array
56+
{
57+
return $form->getConfig()->getOption('help_translation_parameters');
58+
}
59+
5060
public static function isCompound(FormInterface $form): bool
5161
{
5262
return $form->getConfig()->getCompound();

test/Form/HelpMessageType.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
9+
class HelpMessageType extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options)
12+
{
13+
$builder
14+
->add(
15+
'fieldCompany',
16+
TextType::class,
17+
[
18+
'label' => 'Company',
19+
'help' => 'help_message_one',
20+
'help_translation_parameters' => [
21+
'%company%' => 'ACME Inc.',
22+
],
23+
'translation_domain' => 'parent_domain'
24+
]
25+
);
26+
}
27+
}

test/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ services:
9090
tags:
9191
- { name: console.command }
9292

93+
help_message_command:
94+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
95+
arguments:
96+
- Matthias\SymfonyConsoleForm\Tests\Form\HelpMessageType
97+
- help_message
98+
tags:
99+
- { name: console.command }
100+
93101
translatable_label_command:
94102
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
95103
arguments:
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
field_one: Parent first field
22
field_two: Parent second field
3+
help_message_one: Restricted to be %company%

0 commit comments

Comments
 (0)