Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f545150

Browse files
committed
deprecate submitting non HTML5 formatted dates
1 parent acf95c3 commit f545150

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToHtml5LocalDateTimeTransformer.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ class DateTimeToHtml5LocalDateTimeTransformer extends BaseDateTimeTransformer
2222
{
2323
const HTML5_FORMAT = 'Y-m-d\\TH:i:s';
2424

25+
private $strict;
26+
27+
public function __construct(string $inputTimezone = null, string $outputTimezone = null)
28+
{
29+
parent::__construct($inputTimezone, $outputTimezone);
30+
31+
if (2 < \func_num_args()) {
32+
$this->strict = \func_get_arg(2);
33+
} else {
34+
$this->strict = false;
35+
}
36+
37+
if (!$this->strict) {
38+
@trigger_error(sprintf('Parsing dates in %s that are not formatted according to the HTML5 specifications is deprecated since Symfony 4.2.', self::class), E_USER_DEPRECATED);
39+
}
40+
}
41+
2542
/**
2643
* Transforms a \DateTime into a local date and time string.
2744
*
@@ -83,7 +100,13 @@ public function reverseTransform($dateTimeLocal)
83100

84101
// to maintain backwards compatibility we do not strictly validate the submitted date
85102
// see https://github.com/symfony/symfony/issues/28699
86-
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})[T ]\d{2}:\d{2}(?::\d{2})?/', $dateTimeLocal, $matches)) {
103+
if (!$this->strict) {
104+
$regex = '/^(\d{4})-(\d{2})-(\d{2})[T ]\d{2}:\d{2}(?::\d{2})?/';
105+
} else {
106+
$regex = '/^(\d{4})-(\d{2})-(\d{2})[T ]\d{2}:\d{2}(?::\d{2})?$/';
107+
}
108+
109+
if (!preg_match($regex, $dateTimeLocal, $matches)) {
87110
throw new TransformationFailedException(sprintf('The date "%s" is not a valid date.', $dateTimeLocal));
88111
}
89112

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7878
if (self::HTML5_FORMAT === $pattern) {
7979
$builder->addViewTransformer(new DateTimeToHtml5LocalDateTimeTransformer(
8080
$options['model_timezone'],
81-
$options['view_timezone']
81+
$options['view_timezone'],
82+
$options['strict_format']
8283
));
8384
} else {
8485
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
@@ -218,6 +219,7 @@ public function configureOptions(OptionsResolver $resolver)
218219
'model_timezone' => null,
219220
'view_timezone' => null,
220221
'format' => self::HTML5_FORMAT,
222+
'strict_format' => false,
221223
'date_format' => null,
222224
'widget' => null,
223225
'date_widget' => $dateWidget,
@@ -278,6 +280,14 @@ public function configureOptions(OptionsResolver $resolver)
278280
'text',
279281
'choice',
280282
));
283+
284+
$resolver->setNormalizer('strict_format', function (Options $options, $strictFormat) {
285+
if (!$strictFormat && 'single_text' === 'widget' && self::HTML5_FORMAT === $options['format']) {
286+
@trigger_error(sprintf('Setting the "strict_format" option of %s to "false" is deprecated since Symfony 4.2.', self::class), E_USER_DEPRECATED);
287+
}
288+
289+
return $strictFormat;
290+
});
281291
}
282292

283293
/**

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToHtml5LocalDateTimeTransformerTest.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public function reverseTransformProvider()
5151
array('UTC', 'UTC', '2010-02-03 04:05:00 UTC', '2010-02-03T04:05'),
5252
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:00 America/New_York', '2010-02-03T17:05'),
5353
array('Europe/Amsterdam', 'Europe/Amsterdam', '2013-08-21 10:30:00 Europe/Amsterdam', '2013-08-21T10:30:00'),
54-
array('UTC', 'UTC', '2018-09-15T10:00:00Z', '2018-09-15T10:00:00Z'),
55-
array('Europe/Berlin', 'Europe/Berlin', '2018-09-15T10:00:00+02:00', '2018-09-15T10:00:00+02:00'),
56-
array('Europe/Berlin', 'Europe/Berlin', '2018-09-15T10:00:00+0200', '2018-09-15T10:00:00+0200'),
57-
array('UTC', 'UTC', '2018-10-03T10:00:00.000Z', '2018-10-03T10:00:00.000Z'),
5854
);
5955
}
6056

@@ -63,7 +59,7 @@ public function reverseTransformProvider()
6359
*/
6460
public function testTransform($fromTz, $toTz, $from, $to)
6561
{
66-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz);
62+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz, true);
6763

6864
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTime($from) : null));
6965
}
@@ -73,7 +69,7 @@ public function testTransform($fromTz, $toTz, $from, $to)
7369
*/
7470
public function testTransformDateTimeImmutable($fromTz, $toTz, $from, $to)
7571
{
76-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz);
72+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz, true);
7773

7874
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTimeImmutable($from) : null));
7975
}
@@ -83,14 +79,29 @@ public function testTransformDateTimeImmutable($fromTz, $toTz, $from, $to)
8379
*/
8480
public function testTransformRequiresValidDateTime()
8581
{
86-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer();
82+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer(null, null, true);
8783
$transformer->transform('2010-01-01');
8884
}
8985

9086
/**
9187
* @dataProvider reverseTransformProvider
9288
*/
9389
public function testReverseTransform($toTz, $fromTz, $to, $from)
90+
{
91+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($toTz, $fromTz, true);
92+
93+
if (null !== $to) {
94+
$this->assertEquals(new \DateTime($to), $transformer->reverseTransform($from));
95+
} else {
96+
$this->assertNull($transformer->reverseTransform($from));
97+
}
98+
}
99+
100+
/**
101+
* @group legacy
102+
* @dataProvider nonHtml5DatesProvider
103+
*/
104+
public function testReverseTransformNonHtml5Dates($toTz, $fromTz, $to, $from)
94105
{
95106
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($toTz, $fromTz);
96107

@@ -101,12 +112,22 @@ public function testReverseTransform($toTz, $fromTz, $to, $from)
101112
}
102113
}
103114

115+
public function nonHtml5DatesProvider()
116+
{
117+
return array(
118+
array('UTC', 'UTC', '2018-09-15T10:00:00Z', '2018-09-15T10:00:00Z'),
119+
array('Europe/Berlin', 'Europe/Berlin', '2018-09-15T10:00:00+02:00', '2018-09-15T10:00:00+02:00'),
120+
array('Europe/Berlin', 'Europe/Berlin', '2018-09-15T10:00:00+0200', '2018-09-15T10:00:00+0200'),
121+
array('UTC', 'UTC', '2018-10-03T10:00:00.000Z', '2018-10-03T10:00:00.000Z'),
122+
);
123+
}
124+
104125
/**
105126
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
106127
*/
107128
public function testReverseTransformRequiresString()
108129
{
109-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer();
130+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer(null, null, true);
110131
$transformer->reverseTransform(12345);
111132
}
112133

@@ -115,7 +136,7 @@ public function testReverseTransformRequiresString()
115136
*/
116137
public function testReverseTransformWithNonExistingDate()
117138
{
118-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer('UTC', 'UTC');
139+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer('UTC', 'UTC', true);
119140

120141
$transformer->reverseTransform('2010-04-31T04:05');
121142
}
@@ -125,7 +146,7 @@ public function testReverseTransformWithNonExistingDate()
125146
*/
126147
public function testReverseTransformExpectsValidDateString()
127148
{
128-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer('UTC', 'UTC');
149+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer('UTC', 'UTC', true);
129150

130151
$transformer->reverseTransform('2010-2010-2010');
131152
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ public function testSubmitDifferentTimezonesDateTime()
235235
'view_timezone' => 'Pacific/Tahiti',
236236
'widget' => 'single_text',
237237
'input' => 'datetime',
238+
'strict_format' => true,
238239
));
239240

240241
$outputTime = new \DateTime('2010-06-02 03:04:00 Pacific/Tahiti');
@@ -254,6 +255,7 @@ public function testSubmitDifferentTimezonesDateTimeImmutable()
254255
'view_timezone' => 'Pacific/Tahiti',
255256
'widget' => 'single_text',
256257
'input' => 'datetime_immutable',
258+
'strict_format' => true,
257259
));
258260

259261
$outputTime = new \DateTimeImmutable('2010-06-02 03:04:00 Pacific/Tahiti');
@@ -274,6 +276,7 @@ public function testSubmitStringSingleText()
274276
'view_timezone' => 'UTC',
275277
'input' => 'string',
276278
'widget' => 'single_text',
279+
'strict_format' => true,
277280
));
278281

279282
$form->submit('2010-06-02T03:04:00');
@@ -290,6 +293,7 @@ public function testSubmitStringSingleTextWithSeconds()
290293
'input' => 'string',
291294
'widget' => 'single_text',
292295
'with_seconds' => true,
296+
'strict_format' => true,
293297
));
294298

295299
$form->submit('2010-06-02T03:04:05');
@@ -328,6 +332,7 @@ public function testSingleTextWidgetShouldUseTheRightInputType()
328332
{
329333
$view = $this->factory->create(static::TESTED_TYPE, null, array(
330334
'widget' => 'single_text',
335+
'strict_format' => true,
331336
))
332337
->createView();
333338

@@ -453,6 +458,7 @@ public function testPassHtml5TypeIfSingleTextAndHtml5Format()
453458
{
454459
$view = $this->factory->create(static::TESTED_TYPE, null, array(
455460
'widget' => 'single_text',
461+
'strict_format' => true,
456462
))
457463
->createView();
458464

@@ -464,6 +470,7 @@ public function testDontPassHtml5TypeIfHtml5NotAllowed()
464470
$view = $this->factory->create(static::TESTED_TYPE, null, array(
465471
'widget' => 'single_text',
466472
'html5' => false,
473+
'strict_format' => true,
467474
))
468475
->createView();
469476

@@ -621,6 +628,7 @@ public function testSubmitNullWithSingleText()
621628
{
622629
$form = $this->factory->create(static::TESTED_TYPE, null, array(
623630
'widget' => 'single_text',
631+
'strict_format' => true,
624632
));
625633
$form->submit(null);
626634

0 commit comments

Comments
 (0)