8000 [Form] Add options with_minutes to DateTimeType & TimeType · symfony/symfony@bf9e238 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf9e238

Browse files
committed
[Form] Add options with_minutes to DateTimeType & TimeType
1 parent a5e1c4a commit bf9e238

File tree

6 files changed

+157
-18
lines changed

6 files changed

+157
-18
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
{{ block('form_widget_simple') }}
149149
{% else %}
150150
<div {{ block('widget_container_attributes') }}>
151-
{{ form_widget(form.hour, { 'attr': { 'size': '1' } }) }}:{{ form_widget(form.minute, { 'attr': { 'size': '1' } }) }}{% if with_seconds %}:{{ form_widget(form.second, { 'attr': { 'size': '1' } }) }}{% endif %}
151+
{{ form_widget(form.hour, { 'attr': { 'size': '1' } }) }}{% if with_minutes %}:{{ form_widget(form.minute, { 'attr': { 'size': '1' } }) }}{% endif %}{% if with_seconds %}:{{ form_widget(form.second, { 'attr': { 'size': '1' } }) }}{% endif %}
152152
</div>
153153
{% endif %}
154154
{% endspaceless %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/time_widget.html.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
// There should be no spaces between the colons and the widgets, that's why
77
// this block is written in a single PHP tag
88
echo $view['form']->widget($form['hour'], array('attr' => array('size' => 1)));
9-
echo ':';
10-
echo $view['form']->widget($form['minute'], array('attr' => array('size' => 1)));
9+
10+
if ($with_minutes) {
11+
echo ':';
12+
echo $view['form']->widget($form['minute'], array('attr' => array('size' => 1)));
13+
}
1114

1215
if ($with_seconds) {
1316
echo ':';

src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ class DateTimeType extends AbstractType
6969
*/
7070
public function buildForm(FormBuilderInterface $builder, array $options)
7171
{
72-
$parts = array('year', 'month', 'day', 'hour', 'minute');
72+
$parts = array('year', 'month', 'day', 'hour');
7373
$dateParts = array('year', 'month', 'day');
74-
$timeParts = array('hour', 'minute');
74+
$timeParts = array('hour');
75+
76+
if ($options['with_minutes']) {
77+
$parts[] = 'minute';
78+
$timeParts[] = 'minute';
79+
}
7580

7681
if ($options['with_seconds']) {
7782
$parts[] = 'second';
@@ -118,6 +123,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
118123
'hours',
119124
'minutes',
120125
'seconds',
126+
'with_minutes',
121127
'with_seconds',
122128
'empty_value',
123129
'required',
@@ -223,6 +229,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
223229
'widget' => null,
224230
'date_widget' => $dateWidget,
225231
'time_widget' => $timeWidget,
232+
'with_minutes' => true,
226233
'with_seconds' => false,
227234
// Don't modify \DateTime classes by reference, we treat
228235
// them like immutable value objects

src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\FormInterface;
1616
use Symfony\Component\Form\FormBuilderInterface;
1717
use Symfony\Component\Form\ReversedTransformer;
18+
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1819
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
1920
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
2021
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
@@ -29,10 +30,20 @@ class TimeType extends AbstractType
2930
*/
3031
public function buildForm(FormBuilderInterface $builder, array $options)
3132
{
32-
$parts = array('hour', 'minute');
33-
$format = 'H:i';
33+
$parts = array('hour');
34+
$format = 'H';
35+
36+
if ($options['with_seconds'] && !$options['with_minutes']) {
37+
throw new InvalidConfigurationException('You can not disable minutes if you have enabled seconds.');
38+
}
39+
40+
if ($options['with_minutes']) {
41+
$format .= ':i';
42+
$parts[] = 'minute';
43+
}
44+
3445
if ($options['with_seconds']) {
35-
$format = 'H:i:s';
46+
$format .= ':s';
3647
$parts[] = 'second';
3748
}
3849

@@ -49,15 +60,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4960
foreach ($options['hours'] as $hour) {
5061
$hours[$hour] = str_pad($hour, 2, '0', STR_PAD_LEFT);
5162
}
52-
foreach ($options['minutes'] as $minute) {
53-
$minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
54-
}
5563

5664
// Only pass a subset of the options to children
5765
$hourOptions['choices'] = $hours;
5866
$hourOptions['empty_value'] = $options['empty_value']['hour'];
59-
$minuteOptions['choices'] = $minutes;
60-
$minuteOptions['empty_value'] = $options['empty_value']['minute'];
67+
68+
if ($options['with_minutes']) {
69+
foreach ($options['minutes'] as $minute) {
70+
$minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
71+
}
72+
73+
$minuteOptions['choices'] = $minutes;
74+
$minuteOptions['empty_value'] = $options['empty_value']['minute'];
75+
}
6176

6277
if ($options['with_seconds']) {
6378
$seconds = array();
@@ -72,17 +87,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7287

7388
// Append generic carry-along options
7489
foreach (array('required', 'translation_domain') as $passOpt) {
75-
$hourOptions[$passOpt] = $minuteOptions[$passOpt] = $options[$passOpt];
90+
$hourOptions[$passOpt] = $options[$passOpt];
91+
92+
if ($options['with_minutes']) {
93+
$minuteOptions[$passOpt] = $options[$passOpt];
94+
}
95+
7696
if ($options['with_seconds']) {
7797
$secondOptions[$passOpt] = $options[$passOpt];
7898
}
7999
}
80100
}
81101

82-
$builder
83-
->add('hour', $options['widget'], $hourOptions)
84-
->add('minute', $options['widget'], $minuteOptions)
85-
;
102+
$builder->add('hour', $options['widget'], $hourOptions);
103+
104+
if ($options['with_minutes']) {
105+
$builder->add('minute', $options['widget'], $minuteOptions);
106+
}
86107

87108
if ($options['with_seconds']) {
88109
$builder->add('second', $options['widget'], $secondOptions);
@@ -113,6 +134,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
113134
{
114135
$view->vars = array_replace($view->vars, array(
115136
'widget' => $options['widget'],
137+
'with_minutes' => $options['with_minutes'],
116138
'with_seconds' => $options['with_seconds'],
117139
));
118140

@@ -167,6 +189,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
167189
'seconds' => range(0, 59),
168190
'widget' => 'choice',
169191
'input' => 'datetime',
192+
'with_minutes' => true,
170193
'with_seconds' => false,
171194
'model_timezone' => $modelTimezone,
172195
'view_timezone' => $viewTimezone,

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,35 @@ public function testSubmit_timestamp()
9494
$this->assertEquals($dateTime->format('U'), $form->getData());
9595
}
9696

97+
public function testSubmit_withoutMinutes()
98+
{
99+
$form = $this->factory->create('datetime', null, array(
100+
'data_timezone' => 'UTC',
101+
'user_timezone' => 'UTC',
102+
'date_widget' => 'choice',
103+
'time_widget' => 'choice',
104+
'input' => 'datetime',
105+
'with_minutes' => false,
106+
));
107+
108+
$form->setData(new \DateTime('2010-06-02 03:04:05 UTC'));
109+
110+
$input = array(
111+
'date' => array(
112+
'day' => '2',
113+
'month' => '6',
114+
'year' => '2010',
115+
),
116+
'time' => array(
117+
'hour' => '3',
118+
),
119+
);
120+
121+
$form->bind($input);
122+
123+
$this->assertDateTimeEquals(new \DateTime('2010-06-02 03:00:00 UTC'), $form->getData());
124+
}
125+
97126
public function testSubmit_withSeconds()
98127
{
99128
$form = $this->factory->create('datetime', null, array(

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public function testSubmit_datetimeSingleText()
111111
$this->assertEquals('03:04', $form->getViewData());
112112
}
113113

114+
public function testSubmit_datetimeSingleTextWithoutMinutes()
115+
{
116+
$form = $this->factory->create('time', null, array(
117+
'data_timezone' => 'UTC',
118+
'user_timezone' => 'UTC',
119+
'input' => 'datetime',
120+
'widget' => 'single_text',
121+
'with_minutes' => false,
122+
));
123+
124+
$form->bind('03');
125+
126+
$this->assertEquals(new \DateTime('1970-01-01 03:00:00 UTC'), $form->getData());
127+
$this->assertEquals('03', $form->getViewData());
128+
}
129+
114130
public function testSubmit_arraySingleText()
115131
{
116132
$form = $this->factory->create('time', null, array(
@@ -131,6 +147,26 @@ public function testSubmit_arraySingleText()
131147
$this->assertEquals('03:04', $form->getViewData());
132148
}
133149

150+
public function testSubmit_arraySingleTextWithoutMinutes()
151+
{
152+
$form = $this->factory->create('time', null, array(
153+
'data_timezone' => 'UTC',
154+
'user_timezone' => 'UTC',
155+
'input' => 'array',
156+
'widget' => 'single_text',
157+
'with_minutes' => false,
158+
));
159+
160+
$data = array(
161+
'hour' => '3',
162+
);
163+
164+
$form->bind('03');
165+
166+
$this->assertEquals($data, $form->getData());
167+
$this->assertEquals('03', $form->getViewData());
168+
}
169+
134170
public function testSubmit_arraySingleTextWithSeconds()
135171
{
136172
$form = $this->factory->create('time', null, array(
@@ -168,6 +204,36 @@ public function testSubmit_stringSingleText()
168204
$this->assertEquals('03:04', $form->getViewData());
169205
}
170206

207+
public function testSubmit_stringSingleTextWithoutMinutes()
208+
{
209+
$form = $this->factory->create('time', null, array(
210+
'data_timezone' => 'UTC',
211+
'user_timezone' => 'UTC',
212+
'input' => 'string',
213+
'widget' => 'single_text',
214+
'with_minutes' => false,
215+
));
216+
217+
$form->bind('03');
218+
219+
$this->assertEquals('03:00:00', $form->getData());
220+
$this->assertEquals('03', $form->getViewData());
221+
}
222+
223+
public function testSetData_withoutMinutes()
224+
{
225+
$form = $this->factory->create('time', null, array(
226+
'data_timezone' => 'UTC',
227+
'user_timezone' => 'UTC',
228+
'input' => 'datetime',
229+
'with_minutes' => false,
230+
));
231+
232+
$form->setData(new \DateTime('03:04:05 UTC'));
233+
234+
$this->assertEquals(array('hour' => 3), $form->getClientData());
235+
}
236+
171237
public function testSetData_withSeconds()
172238
{
173239
$form = $this->factory->create('time', null, array(
@@ -561,4 +627,15 @@ public function testSecondErrorsBubbleUp($widget)
561627
$this->assertSame(array(), $form['second']->getErrors());
562628
$this->assertSame(array($error), $form->getErrors());
563629
}
630+
631+
/**
632+
* @expectedException Symfony\Component\Form\Exception\InvalidConfigurationException
633+
*/
634+
public function testInitializeWithSecondsAndWithoutMinutes()
635+
{
636+
$this->factory->create('time', null, array(
637+
'with_minutes' => false,
638+
'with_seconds' => true,
639+
));
640+
}
564641
}

0 commit comments

Comments
 (0)
0