diff --git a/lib/Cake/Model/Validator/CakeValidationRule.php b/lib/Cake/Model/Validator/CakeValidationRule.php index 1963de4b4fb..ab95b5693f1 100644 --- a/lib/Cake/Model/Validator/CakeValidationRule.php +++ b/lib/Cake/Model/Validator/CakeValidationRule.php @@ -130,6 +130,15 @@ public function isValid() { return true; } +/** + * Returns whether the field can be left blank according to this rule + * + * @return boolean + */ + public function isEmptyAllowed() { + return $this->skip() || $this->allowEmpty === true; + } + /** * Checks if the field is required according to the `required` property * diff --git a/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php b/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php index c72f4969fc8..85d2350e52d 100644 --- a/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php +++ b/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php @@ -132,4 +132,33 @@ public function testIsRequired() { $Rule->isUpdate(true); $this->assertTrue($Rule->isRequired()); } + +/** + * Test isEmptyAllowed method + * + * @return void + */ + public function testIsEmplyAllowed() { + $def = array('rule' => 'aRule', 'allowEmpty' => true); + $Rule = new CakeValidationRule($def); + $this->assertTrue($Rule->isEmptyAllowed()); + + $def = array('rule' => 'aRule', 'allowEmpty' => false); + $Rule = new CakeValidationRule($def); + $this->assertFalse($Rule->isEmptyAllowed()); + + $def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'update'); + $Rule = new CakeValidationRule($def); + $this->assertTrue($Rule->isEmptyAllowed()); + + $Rule->isUpdate(true); + $this->assertFalse($Rule->isEmptyAllowed()); + + $def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'create'); + $Rule = new CakeValidationRule($def); + $this->assertFalse($Rule->isEmptyAllowed()); + + $Rule->isUpdate(true); + $this->assertTrue($Rule->isEmptyAllowed()); + } }