diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index 1799964ba11..25a25bcd78b 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -525,6 +525,10 @@ protected function _fieldIsEmpty($data) if (empty($data) && $data !== '0' && $data !== false && $data !== 0 && $data !== 0.0) { return true; } + if (is_array($data) && (isset($data['year']) || isset($data['hour']))) { + $value = implode('', $data); + return strlen($value) === 0; + } return false; } diff --git a/tests/TestCase/Validation/ValidatorTest.php b/tests/TestCase/Validation/ValidatorTest.php index a4198dd5d22..3e5d1e69bd5 100644 --- a/tests/TestCase/Validation/ValidatorTest.php +++ b/tests/TestCase/Validation/ValidatorTest.php @@ -195,6 +195,52 @@ public function testAllowEmpty() $this->assertEquals('update', $validator->field('title')->isEmptyAllowed()); } + /** + * Tests the allowEmpty method with date/time fields. + * + * @return void + */ + public function testAllowEmptyDateTime() + { + $validator = new Validator; + $validator->allowEmpty('created') + ->add('created', 'date', ['rule' => 'date']); + + $data = [ + 'created' => [ + 'year' => '', + 'month' => '', + 'day' => '' + ] + ]; + $result = $validator->errors($data); + $this->assertEmpty($result, 'No errors on empty date'); + + $data = [ + 'created' => [ + 'year' => '', + 'month' => '', + 'day' => '', + 'hour' => '', + 'minute' => '', + 'second' => '', + 'meridian' => '', + ] + ]; + $result = $validator->errors($data); + $this->assertEmpty($result, 'No errors on empty datetime'); + + $data = [ + 'created' => [ + 'hour' => '', + 'minute' => '', + 'meridian' => '', + ] + ]; + $result = $validator->errors($data); + $this->assertEmpty($result, 'No errors on empty time'); + } + /** * Test the notEmpty() method. *