File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -399,3 +399,23 @@ Feature: It is possible to interactively fill in a form from the CLI
399399 [0] => blue
400400 )
401401 """
402+
403+ Scenario : Default value with transformers
404+ Given I run the command "form:default_value_with_data_transformers" and I provide as input "Rue du Faubourg Saint-Honoré" with parameters
405+ | Parameter | Value |
406+ | --street | pennsylvania -ave -nw |
407+ Then the command has finished successfully
408+ And the output should contain
409+ """
410+ Street [pennsylvania-ave-nw]:
411+ """
412+ And the output should contain
413+ """
414+ Array
415+ (
416+ [street] => Matthias\SymfonyConsoleForm\Tests\Form\Data\Street Object
417+ (
418+ [value] => Rue du Faubourg Saint-Honoré
419+ )
420+ )
421+ """
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Matthias \SymfonyConsoleForm \Tests \Form \Data ;
4+
5+ class Street implements \Stringable
6+ {
7+ public string $ value ;
8+
9+ public function __construct (string $ street )
10+ {
11+ $ this ->value = $ street ;
12+ }
13+
14+ public function __toString (): string
15+ {
16+ return $ this ->value ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Matthias \SymfonyConsoleForm \Tests \Form ;
4+
5+ use Matthias \SymfonyConsoleForm \Tests \Form \Data \Address ;
6+ use Matthias \SymfonyConsoleForm \Tests \Form \Data \Street ;
7+ use Symfony \Component \Form \AbstractType ;
8+ use Symfony \Component \Form \CallbackTransformer ;
9+ use Symfony \Component \Form \Extension \Core \Type \ChoiceType ;
10+ use Symfony \Component \Form \Extension \Core \Type \TextType ;
11+ use Symfony \Component \Form \FormBuilderInterface ;
12+
13+ class DefaultValueWithDataTransformersType extends AbstractType
14+ {
15+ public function buildForm (FormBuilderInterface $ builder , array $ options )
16+ {
17+ $ builder
18+ ->add ('street ' , StreetType::class, [
19+ 'label ' => 'Street '
20+ ]);
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Matthias \SymfonyConsoleForm \Tests \Form ;
4+
5+ use Matthias \SymfonyConsoleForm \Tests \Form \Data \Address ;
6+ use Matthias \SymfonyConsoleForm \Tests \Form \Data \Street ;
7+ use Symfony \Component \Form \AbstractType ;
8+ use Symfony \Component \Form \DataTransformerInterface ;
9+ use Symfony \Component \Form \Exception \TransformationFailedException ;
10+ use Symfony \Component \Form \Extension \Core \Type \TextType ;
11+ use Symfony \Component \Form \FormBuilderInterface ;
12+ use Symfony \Component \OptionsResolver \OptionsResolver ;
13+
14+ class StreetType extends AbstractType implements DataTransformerInterface
15+ {
16+ public function buildForm (FormBuilderInterface $ builder , array $ options )
17+ {
18+ $ builder ->addModelTransformer ($ this );
19+ }
20+
21+ public function getParent ()
22+ {
23+ return TextType::class;
24+ }
25+
26+ public function transform (mixed $ value )
27+ {
28+ return (string )$ value ;
29+ }
30+
31+ public function reverseTransform (mixed $ value )
32+ {
33+ return new Street ((string )$ value );
34+ }
35+ }
Original file line number Diff line number Diff line change @@ -154,6 +154,14 @@ services:
154154 tags :
155155 - { name: console.command }
156156
157+ default_value_with_data_transformers :
158+ class : Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
159+ arguments :
160+ - Matthias\SymfonyConsoleForm\Tests\Form\DefaultValueWithDataTransformersType
161+ - default_value_with_data_transformers
162+ tags :
163+ - { name: console.command }
164+
157165 nested_command :
158166 class : Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
159167 arguments :
You can’t perform that action at this time.
0 commit comments