Skip to content
Permalink
Browse files

Accept any DateTimeInterface as valid.

This fixes validators not accepting immutable datetime objects.

Refs #8333
  • Loading branch information...
markstory committed Feb 25, 2016
1 parent bca53dc commit f3462c06bae748986da5d6350e2df917f64ab994
Showing with 12 additions and 7 deletions.
  1. +7 −7 src/Validation/Validation.php
  2. +5 −0 tests/TestCase/Validation/ValidationTest.php
@@ -15,7 +15,7 @@
namespace Cake\Validation;
use Cake\Utility\Text;
use DateTime;
use DateTimeInterface;
use LogicException;
use NumberFormatter;
use RuntimeException;
@@ -347,15 +347,15 @@ public static function custom($check, $regex = null)
* - `ym` 2006/12 or 06/12 separators can be a space, period, dash, forward slash
* - `y` 2006 just the year without any separators
*
* @param string|\DateTime $check a valid date string/object
* @param string|\DateTimeInterface $check a valid date string/object
* @param string|array $format Use a string or an array of the keys above.
* Arrays should be passed as ['dmy', 'mdy', etc]
* @param string|null $regex If a custom regular expression is used this is the only validation that will occur.
* @return bool Success
*/
public static function date($check, $format = 'ymd', $regex = null)
{
if ($check instanceof DateTime) {
if ($check instanceof DateTimeInterface) {
return true;
}
@@ -411,7 +411,7 @@ public static function date($check, $format = 'ymd', $regex = null)
*
* All values matching the "date" core validation rule, and the "time" one will be valid
*
* @param string|\DateTime $check Value to check
* @param string|\DateTimeInterface $check Value to check
* @param string|array $dateFormat Format of the date part. See Validation::date() for more information.
* @param string|null $regex Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
* @return bool True if the value is valid, false otherwise
@@ -420,7 +420,7 @@ public static function date($check, $format = 'ymd', $regex = null)
*/
public static function datetime($check, $dateFormat = 'ymd', $regex = null)
{
if ($check instanceof DateTime) {
if ($check instanceof DateTimeInterface) {
return true;
}
$valid = false;
@@ -442,12 +442,12 @@ public static function datetime($check, $dateFormat = 'ymd', $regex = null)
* Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
* Does not allow/validate seconds.
*
* @param string|\DateTime $check a valid time string/object
* @param string|\DateTimeInterface $check a valid time string/object
* @return bool Success
*/
public static function time($check)
{
if ($check instanceof DateTime) {
if ($check instanceof DateTimeInterface) {
return true;
}
if (is_array($check)) {
@@ -900,6 +900,11 @@ public function testDateTimeObject()
$this->assertTrue(Validation::date($dateTime));
$this->assertTrue(Validation::time($dateTime));
$this->assertTrue(Validation::dateTime($dateTime));
$dateTime = new \DateTimeImmutable();
$this->assertTrue(Validation::date($dateTime));
$this->assertTrue(Validation::time($dateTime));
$this->assertTrue(Validation::dateTime($dateTime));
}
/**

0 comments on commit f3462c0

Please sign in to comment.
You can’t perform that action at this time.