Skip to content

Commit 7ffad2b

Browse files
committed
deprecate custom formats with HTML5 widgets
1 parent bcff647 commit 7ffad2b

File tree

7 files changed

+44
-1
lines changed

7 files changed

+44
-1
lines changed

UPGRADE-4.2.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Finder
5555
Form
5656
----
5757

58+
* Using the `format` option of the `DateType` and `DateTimeType` when the `html5` option is enabled is deprecated.
5859
* The `getExtendedType()` method of the `FormTypeExtensionInterface` is deprecated and will be removed in 5.0. Type
5960
extensions must implement the static `getExtendedTypes()` method instead and return an iterable of extended types.
6061

UPGRADE-5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Finder
7272
Form
7373
----
7474

75+
* Removed support for using the `format` option of the `DateType` and `DateTimeType` when the `html5` option is enabled.
7576
* The `getExtendedType()` method was removed from the `FormTypeExtensionInterface`. It is replaced by the the static
7677
`getExtendedTypes()` method which must return an iterable of extended types.
7778

src/Symfony/Component/Form/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* deprecated using the `format` option of the `DateType` and `DateTimeType` when the `html5` option is enabled
78
* The `getExtendedType()` method of the `FormTypeExtensionInterface` is deprecated and will be removed in 5.0. Type
89
extensions must implement the static `getExtendedTypes()` method instead and return an iterable of extended types.
910

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

+13
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
148148
$dateOptions['input'] = $timeOptions['input'] = 'array';
149149
$dateOptions['error_bubbling'] = $timeOptions['error_bubbling'] = true;
150150

151+
if (isset($dateOptions['format']) && DateType::HTML5_FORMAT !== $dateOptions['format']) {
152+
$dateOptions['html5'] = false;
153+
}
154+
151155
$builder
152156
->addViewTransformer(new DataTransformerChain(array(
153157
new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts),
@@ -278,6 +282,15 @@ public function configureOptions(OptionsResolver $resolver)
278282
'text',
279283
'choice',
280284
));
285+
286+
$resolver->setNormalizer('html5', function (Options $options, $html5) {
287+
if ($html5 && self::HTML5_FORMAT !== $options['format']) {
288+
@trigger_error(sprintf('Using a custom format when the "html5" option of the %s is enabled is deprecated since Symfony 4.2 and will lead to an exception in 5.0..', self::class), E_USER_DEPRECATED);
289+
//throw new LogicException(sprintf('Cannot use the "format" option of the %s when the "html5" option is disabled.', self::class));
290+
}
291+
292+
return $html5;
293+
});
281294
}
282295

283296
/**

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function configureOptions(OptionsResolver $resolver)
241241
};
242242

243243
$format = function (Options $options) {
244-
return 'single_text' === $options['widget'] ? DateType::HTML5_FORMAT : DateType::DEFAULT_FORMAT;
244+
return 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;
245245
};
246246

247247
$resolver->setDefaults(array(
@@ -288,6 +288,15 @@ public function configureOptions(OptionsResolver $resolver)
288288
$resolver->setAllowedTypes('years', 'array');
289289
$resolver->setAllowedTypes('months', 'array');
290290
$resolver->setAllowedTypes('days', 'array');
291+
292+
$resolver->setNormalizer('html5', function (Options $options, $html5) {
293+
if ($html5 && 'single_text' === $options['widget'] && self::HTML5_FORMAT !== $options['format']) {
294+
@trigger_error(sprintf('Using a custom format when the "html5" option of the %s is enabled is deprecated since Symfony 4.2 and will lead to an exception in 5.0..', self::class), E_USER_DEPRECATED);
295+
//throw new LogicException(sprintf('Cannot use the "format" option of the %s when the "html5" option is disabled.', self::class));
296+
}
297+
298+
return $html5;
299+
});
291300
}
292301

293302
/**

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

+3
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ public function testDontPassHtml5TypeIfHtml5NotAllowed()
470470
$this->assertArrayNotHasKey('type', $view->vars);
471471
}
472472

473+
/**
474+
* @group legacy
475+
*/
473476
public function testDontPassHtml5TypeIfNotHtml5Format()
474477
{
475478
$view = $this->factory->create(static::TESTED_TYPE, null, array(

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

+15
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function testSubmitFromSingleTextDateTimeWithCustomFormat()
7676
'widget' => 'single_text',
7777
'input' => 'datetime',
7878
'format' => 'yyyy',
79+
'html5' => false,
7980
));
8081

8182
$form->submit('2010');
@@ -93,6 +94,7 @@ public function testSubmitFromSingleTextDateTime()
9394

9495
$form = $this->factory->create(static::TESTED_TYPE, null, array(
9596
'format' => \IntlDateFormatter::MEDIUM,
97+
'html5' => false,
9698
'model_timezone' => 'UTC',
9799
'view_timezone' => 'UTC',
98100
'widget' => 'single_text',
@@ -114,6 +116,7 @@ public function testSubmitFromSingleTextDateTimeImmutable()
114116

115117
$form = $this->factory->create(static::TESTED_TYPE, null, array(
116118
'format' => \IntlDateFormatter::MEDIUM,
119+
'html5' => false,
117120
'model_timezone' => 'UTC',
118121
'view_timezone' => 'UTC',
119122
'widget' => 'single_text',
@@ -136,6 +139,7 @@ public function testSubmitFromSingleTextString()
136139

137140
$form = $this->factory->create(static::TESTED_TYPE, null, array(
138141
'format' => \IntlDateFormatter::MEDIUM,
142+
'html5' => false,
139143
'model_timezone' => 'UTC',
140144
'view_timezone' => 'UTC',
141145
'widget' => 'single_text',
@@ -157,6 +161,7 @@ public function testSubmitFromSingleTextTimestamp()
157161

158162
$form = $this->factory->create(static::TESTED_TYPE, null, array(
159163
'format' => \IntlDateFormatter::MEDIUM,
164+
'html5' => false,
160165
'model_timezone' => 'UTC',
161166
'view_timezone' => 'UTC',
162167
'widget' => 'single_text',
@@ -180,6 +185,7 @@ public function testSubmitFromSingleTextRaw()
180185

181186
$form = $this->factory->create(static::TESTED_TYPE, null, array(
182187
'format' => \IntlDateFormatter::MEDIUM,
188+
'html5' => false,
183189
'model_timezone' => 'UTC',
184190
'view_timezone' => 'UTC',
185191
'widget' => 'single_text',
@@ -270,6 +276,7 @@ public function testSubmitFromInputDateTimeDifferentPattern()
270276
'model_timezone' => 'UTC',
271277
'view_timezone' => 'UTC',
272278
'format' => 'MM*yyyy*dd',
279+
'html5' => false,
273280
'widget' => 'single_text',
274281
'input' => 'datetime',
275282
));
@@ -286,6 +293,7 @@ public function testSubmitFromInputStringDifferentPattern()
286293
'model_timezone' => 'UTC',
287294
'view_timezone' => 'UTC',
288295
'format' => 'MM*yyyy*dd',
296+
'html5' => false,
289297
'widget' => 'single_text',
290298
'input' => 'string',
291299
));
@@ -302,6 +310,7 @@ public function testSubmitFromInputTimestampDifferentPattern()
302310
'model_timezone' => 'UTC',
303311
'view_timezone' => 'UTC',
304312
'format' => 'MM*yyyy*dd',
313+
'html5' => false,
305314
'widget' => 'single_text',
306315
'input' => 'timestamp',
307316
));
@@ -320,6 +329,7 @@ public function testSubmitFromInputRawDifferentPattern()
320329
'model_timezone' => 'UTC',
321330
'view_timezone' => 'UTC',
322331
'format' => 'MM*yyyy*dd',
332+
'html5' => false,
323333
'widget' => 'single_text',
324334
'input' => 'array',
325335
));
@@ -368,6 +378,7 @@ public function testThrowExceptionIfFormatIsNoPattern()
368378
{
369379
$this->factory->create(static::TESTED_TYPE, null, array(
370380
'format' => '0',
381+
'html5' => false,
371382
'widget' => 'single_text',
372383
'input' => 'string',
373384
));
@@ -394,6 +405,7 @@ public function testThrowExceptionIfFormatMissesYearMonthAndDayWithSingleTextWid
394405
$this->factory->create(static::TESTED_TYPE, null, array(
395406
'widget' => 'single_text',
396407
'format' => 'wrong',
408+
'html5' => false,
397409
));
398410
}
399411

@@ -456,6 +468,7 @@ public function testSetDataWithNegativeTimezoneOffsetStringInput()
456468

457469
$form = $this->factory->create(static::TESTED_TYPE, null, array(
458470
'format' => \IntlDateFormatter::MEDIUM,
471+
'html5' => false,
459472
'model_timezone' => 'UTC',
460473
'view_timezone' => 'America/New_York',
461474
'input' => 'string',
@@ -478,6 +491,7 @@ public function testSetDataWithNegativeTimezoneOffsetDateTimeInput()
478491

479492
$form = $this->factory->create(static::TESTED_TYPE, null, array(
480493
'format' => \IntlDateFormatter::MEDIUM,
494+
'html5' => false,
481495
'model_timezone' => 'UTC',
482496
'view_timezone' => 'America/New_York',
483497
'input' => 'datetime',
@@ -856,6 +870,7 @@ public function testDontPassHtml5TypeIfNotHtml5Format()
856870
$view = $this->factory->create(static::TESTED_TYPE, null, array(
857871
'widget' => 'single_text',
858872
'format' => \IntlDateFormatter::MEDIUM,
873+
'html5' => false,
859874
))
860875
->createView();
861876

0 commit comments

Comments
 (0)