From 8aa6ad1c5de5eb5473139a2b8ecc15d37b891544 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Mon, 14 Jul 2014 14:02:34 +0200 Subject: [PATCH] Allow date to be DateTime object --- src/Valitron/Validator.php | 8 +++++++- tests/Valitron/ValidateTest.php | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 13bff59..0a96bf2 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -495,7 +495,13 @@ class Validator */ protected function validateDate($field, $value) { - return strtotime($value) !== false; + $isDate = false; + if($value instanceof \DateTime) { + $isDate = true; + } else { + $isDate = strtotime($value) !== false; + } + return $isDate; } /** diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 4d2c2c6..d1ae011 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -413,6 +413,13 @@ class ValidateTest extends BaseTestCase $this->assertTrue($v->validate()); } + public function testDateValidWithDateTimeObject() + { + $v = new Validator(array('date' => new DateTime())); + $v->rule('date', 'date'); + $this->assertTrue($v->validate()); + } + public function testDateInvalid() { $v = new Validator(array('date' => 'no thanks'));