Skip to content

Commit 4f06f15

Browse files
committed
Add stricter checking for valid date time string
1 parent 253d0a6 commit 4f06f15

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function reverseTransform($dateTimeLocal)
7676
return;
7777
}
7878

79+
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})[T ]\d{2}:\d{2}(?::\d{2})?$/', $dateTimeLocal, $matches)) {
80+
throw new TransformationFailedException(sprintf('The date "%s" is not a valid date.', $dateTimeLocal));
81+
}
82+
7983
try {
8084
$dateTime = new \DateTime($dateTimeLocal, new \DateTimeZone($this->outputTimezone));
8185
} catch (\Exception $e) {
@@ -86,10 +90,8 @@ public function reverseTransform($dateTimeLocal)
8690
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
8791
}
8892

89-
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $dateTimeLocal, $m)) {
90-
if (!checkdate($m[2], $m[3], $m[1])) {
91-
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $m[1], $m[2], $m[3]));
92-
}
93+
if (!checkdate($matches[2], $matches[3], $matches[1])) {
94+
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
9395
}
9496

9597
return $dateTime;

0 commit comments

Comments
 (0)