diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a2cd2..b081880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Valdi Changelog Released: Upcoming +## 1.0.2 + +Released: 2021-03-11 + +- The dateTime validator don't accept invalid dates like 2021-02-30 anymore + ## 1.0.1 Released: 2021-03-04 diff --git a/src/Valdi/Validator/DateTime.php b/src/Valdi/Validator/DateTime.php index ba65115..d4c4d9a 100644 --- a/src/Valdi/Validator/DateTime.php +++ b/src/Valdi/Validator/DateTime.php @@ -28,8 +28,11 @@ public function isValid($value, array $parameters) if (count($parameters) > 0) { $format = $parameters[0]; } - return in_array($value, ['', null], true) || - \DateTime::createFromFormat($format, $value) !== false; + if (in_array($value, ['', null], true)) { + return true; + } + $dateTime = \DateTime::createFromFormat($format, $value); + return $dateTime && $dateTime->format($format) === $value; } /** diff --git a/tests/ValdiTests/Validator/DateTimeTest.php b/tests/ValdiTests/Validator/DateTimeTest.php index cb8be5e..fe837d6 100644 --- a/tests/ValdiTests/Validator/DateTimeTest.php +++ b/tests/ValdiTests/Validator/DateTimeTest.php @@ -26,6 +26,8 @@ public function testValidate() $this->assertFalse($validator->isValid('2016-02-28', [])); $this->assertFalse($validator->isValid('test', [])); + $this->assertFalse($validator->isValid('2016-02-30 01:23:45', [])); + $this->assertFalse($validator->isValid('2016-02-28 25:23:45', [])); $this->assertTrue($validator->isValid('', [])); $this->assertTrue($validator->isValid(null, []));