Skip to content

Commit

Permalink
Implemented new method isEmptyAllowed in CakeValidationRule
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 11, 2012
1 parent 785cf83 commit 217bf85
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Cake/Model/Validator/CakeValidationRule.php
Expand Up @@ -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
*
Expand Down
29 changes: 29 additions & 0 deletions lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php
Expand Up @@ -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());
}
}

0 comments on commit 217bf85

Please sign in to comment.