diff --git a/lib/Cake/Model/Validator/CakeValidationRule.php b/lib/Cake/Model/Validator/CakeValidationRule.php index 6380376bf..3ce7c2fcd 100644 --- a/lib/Cake/Model/Validator/CakeValidationRule.php +++ b/lib/Cake/Model/Validator/CakeValidationRule.php @@ -163,9 +163,9 @@ class CakeValidationRule { */ public function checkRequired($field, &$data) { return ( - (!isset($data[$field]) && $this->isRequired() === true) || + (!array_key_exists($field, $data) && $this->isRequired() === true) || ( - isset($data[$field]) && (empty($data[$field]) && + array_key_exists($field, $data) && (empty($data[$field]) && !is_numeric($data[$field])) && $this->allowEmpty === false ) ); diff --git a/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php b/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php index e0d9c8b35..7a43f32f5 100644 --- a/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php +++ b/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php @@ -171,4 +171,30 @@ class CakeValidationRuleTest extends CakeTestCase { $Rule->isUpdate(true); $this->assertTrue($Rule->isEmptyAllowed()); } + +/** + * Test checkRequired method + * + * @return void + */ + public function testCheckRequiredWhenRequiredAndAllowEmpty() { + + $Rule = $this->getMock('CakeValidationRule', array('isRequired')); + $Rule->expects($this->any()) + ->method('isRequired') + ->will($this->returnValue(true)); + $Rule->allowEmpty = true; + + $fieldname = 'field'; + $data = array( + $fieldname => null + ); + + $this->assertFalse($Rule->checkRequired($fieldname, $data), "A null but present field should not fail requirement check if allowEmpty is true"); + + $Rule->allowEmpty = false; + + $this->assertTrue($Rule->checkRequired($fieldname, $data), "A null but present field should fail requirement check if allowEmpty is false"); + + } }