Skip to content

Commit

Permalink
Version 1.0.2 released! The dateTime validator don't accept invalid d…
Browse files Browse the repository at this point in the history
…ates like 2021-02-30 anymore.
  • Loading branch information
philiplb committed Mar 11, 2021
1 parent 0333fa0 commit 63a2c54
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/Valdi/Validator/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/ValdiTests/Validator/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, []));
Expand Down

0 comments on commit 63a2c54

Please sign in to comment.