8000 [Form] Improved DateTimeType code · symfony/symfony@d621a76 · GitHub
[go: up one dir, main page]

Skip to content

Commit d621a76

Browse files
committed
[Form] Improved DateTimeType code
1 parent 5f5efaf commit d621a76

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4343
}
4444

4545
if ('single_text' === $options['widget']) {
46-
$builder->addViewTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format));
46+
$builder->addViewTransformer(new DateTimeToStringTransformer(
47+
$options['data_timezone'],
48+
$options['user_timezone'],
49+
$format
50+
));
4751
} else {
4852
// Only pass a subset of the options to children
4953
$dateOptions = array_intersect_key($options, array_flip(array(
@@ -54,6 +58,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5458
'required',
5559
'translation_domain',
5660
)));
61+
5762
$timeOptions = array_intersect_key($options, array_flip(array(
5863
'hours',
5964
'minutes',
@@ -64,21 +69,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6469
'translation_domain',
6570
)));
6671

67-
// If `widget` is set, overwrite widget options from `date` and `time`
68-
if (isset($options['widget'])) {
69-
$dateOptions['widget'] = $options['widget'];
70-
$timeOptions['widget'] = $options['widget'];
71-
} else {
72-
if (isset($options['date_widget'])) {
73-
$dateOptions['widget'] = $options['date_widget'];
74-
}
75-
76-
if (isset($options['time_widget'])) {
77-
$timeOptions['widget'] = $options['time_widget'];
78-
}
72+
if (null !== $options['date_widget']) {
73+
$dateOptions['widget'] = $options['date_widget'];
7974
}
8075

81-
if (isset($options['date_format'])) {
76+
if (null !== $options['time_widget']) {
77+
$timeOptions['widget'] = $options['time_widget'];
78+
}
79+
80+
if (null !== $options['date_format']) {
8281
$dateOptions['format'] = $options['date_format'];
8382
}
8483

@@ -134,27 +133,28 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
134133
return $options['widget'] !== 'single_text';
135134
};
136135

136+
// Defaults to the value of "widget"
137+
$dateWidget = function (Options $options) {
138+
return $options['widget'];
139+
};
140+
141+
// Defaults to the value of "widget"
142+
$timeWidget = function (Options $options) {
143+
return $options['widget'];
144+
};
145+
137146
$resolver->setDefaults(array(
138147
'input' => 'datetime',
139148
'data_timezone' => null,
140149
'user_timezone' => null,
141-
'date_widget' => null,
142150
'date_format' => null,
143-
'time_widget' => null,
144-
/* Defaults for date field */
145-
'years' => range(date('Y') - 5, date('Y') + 5),
146-
'months' => range(1, 12),
147-
'days' => range(1, 31),
148-
/* Defaults for time field */
149-
'hours' => range(0, 23),
150-
'minutes' => range(0, 59),
151-
'seconds' => range(0, 59),
151+
'widget' => null,
152+
'date_widget' => $dateWidget,
153+
'time_widget' => $timeWidget,
152154
'with_seconds' => false,
153155
// Don't modify \DateTime classes by reference, we treat
154156
// them like immutable value objects
155157
'by_reference' => false,
156-
// This will overwrite "widget" child options
157-
'widget' => null,
158158
// If initialized with a \DateTime object, FormType initializes
159159
// this option to "\DateTime". Since the internal, normalized
160160
// representation is not \DateTime, but an array, we need to unset
@@ -167,6 +167,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
167167
// set in DateType and TimeType
168168
$resolver->setOptional(array(
169169
'empty_value',
170+
'years',
171+
'months',
172+
'days',
173+
'hours',
174+
'minutes',
175+
'seconds',
170176
));
171177

172178
$resolver->setAllowedValues(array(

0 commit comments

Comments
 (0)
0