Skip to content

Commit 2d23378

Browse files
Merge branch 'pawaclawczyk-show-label-for-date-time'
2 parents d8ae9e9 + 6ee1ff4 commit 2d23378

File tree

8 files changed

+98
-0
lines changed

8 files changed

+98
-0
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class FeatureContext implements Context, SnippetAcceptingContext
3333
*/
3434
public function __construct()
3535
{
36+
ini_set('date.timezone', 'UTC');
37+
3638
$kernel = new AppKernel('test', true);
3739
$this->application = new Application($kernel);
3840
$this->tester = new ApplicationTester($this->application);

features/interactive.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ Feature: It is possible to interactively fill in a form from the CLI
3636
This value is too short.
3737
"""
3838

39+
Scenario: Provide a date as text
40+
When I run the command "form:date_of_birth" and I provide as input
41+
"""
42+
2015-03-04[enter]
43+
"""
44+
Then the command has finished successfully
45+
And the output should be
46+
"""
47+
Your date of birth [1879-03-14]: Array
48+
(
49+
[dateOfBirth] => DateTime Object
50+
(
51+
[date] => 2015-03-04 00:00:00.000000
52+
[timezone_type] => 1
53+
[timezone] => +00:00
54+
)
55+
)
56+
"""
57+
3958
Scenario: Select a value
4059
When I run the command "form:color" and I provide as input
4160
"""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Bridge\Transformer;
4+
5+
use Symfony\Component\Form\FormInterface;
6+
7+
class DateTimeTransformer extends TextTransformer
8+
{
9+
protected function defaultValueFrom(FormInterface $form)
10+
{
11+
return $form->getViewData();
12+
}
13+
}

src/Bundle/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ services:
3434
tags:
3535
- { name: form_to_question_transformer, form_type: text }
3636

37+
matthias_symfony_console_form.date_time_transformer:
38+
class: Matthias\SymfonyConsoleForm\Bridge\Transformer\DateTimeTransformer
39+
public: false
40+
tags:
41+
- { name: form_to_question_transformer, form_type: time }
42+
- { name: form_to_question_transformer, form_type: date }
43+
- { name: form_to_question_transformer, form_type: datetime }
44+
3745
matthias_symfony_console_form.password_transformer:
3846
class: Matthias\SymfonyConsoleForm\Bridge\Transformer\PasswordTransformer
3947
public: false

test/Form/Data/Demo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class Demo
88
public $email;
99
public $country;
1010
public $addresses;
11+
public $dateOfBirth;
1112
}

test/Form/DateOfBirthType.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
8+
class DateOfBirthType extends AbstractType
9+
{
10+
public function buildForm(FormBuilderInterface $builder, array $options)
11+
{
12+
$builder
13+
->add(
14+
'dateOfBirth',
15+
'date',
16+
[
17+
'label' => 'Your date of birth',
18+
'data' => new \DateTime('1879-03-14'),
19+
'widget' => 'single_text',
20+
]
21+
)->add(
22+
'submit',
23+
'submit',
24+
[
25+
'label' => 'Submit',
26+
]
27+
);
28+
}
29+
30+
public function getName()
31+
{
32+
return 'date_of_birth';
33+
}
34+
}

test/Form/DemoType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5151
],
5252
]
5353
)
54+
->add(
55+
'dateOfBirth',
56+
'date',
57+
[
58+
'label' => 'Your date of birth',
59+
'data' => new \DateTime('1879-03-14'),
60+
'widget' => 'single_text',
61+
]
62+
)
5463
;
5564
}
5665

test/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ services:
3737
tags:
3838
- { name: console.command }
3939

40+
date_of_birth_command:
41+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
42+
arguments:
43+
- date_of_birth
44+
tags:
45+
- { name: console.command }
46+
4047
demo_form_type:
4148
class: Matthias\SymfonyConsoleForm\Tests\Form\DemoType
4249
tags:
@@ -62,6 +69,11 @@ services:
6269
tags:
6370
- { name: form.type, alias: repeated_password }
6471

72+
date_of_birth_form_type:
73+
class: Matthias\SymfonyConsoleForm\Tests\Form\DateOfBirthType
74+
tags:
75+
- { name: form.type, alias: date_of_birth }
76+
6577
framework:
6678
form:
6779
csrf_protection: true

0 commit comments

Comments
 (0)