Skip to content

Commit

Permalink
Fix rules set being updated with array instead of CakeValidationRule …
Browse files Browse the repository at this point in the history
…objects. Closes #3367
  • Loading branch information
ADmad committed Nov 13, 2012
1 parent e0586da commit ac087ec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/Cake/Model/Validator/CakeValidationSet.php
Expand Up @@ -192,7 +192,7 @@ public function getRules() {
* @return CakeValidationSet this instance
*/
public function setRule($name, $rule) {
if (!$rule instanceof CakeValidationRule) {
if (!($rule instanceof CakeValidationRule)) {
$rule = new CakeValidationRule($rule);
}
$this->_rules[$name] = $rule;
Expand Down Expand Up @@ -236,9 +236,10 @@ public function removeRule($name) {
*/
public function setRules($rules = array(), $mergeVars = true) {
if ($mergeVars === false) {
$this->_rules = $rules;
} else {
$this->_rules = array_merge($this->_rules, $rules);
$this->_rules = array();
}
foreach ($rules as $name => $rule) {
$this->setRule($name, $rule);
}
return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php
Expand Up @@ -156,10 +156,22 @@ public function testSetRules() {
$result = $Field->getRules();
$this->assertEquals(array('validEmail'), array_keys($result));

$Field->setRules(array('validEmail' => $rule), false);
$result = $Field->getRules();
$this->assertEquals(array('validEmail'), array_keys($result));
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);

$rules = array('notEmpty' => $RuleEmpty);
$Field->setRules($rules, true);
$result = $Field->getRules();
$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));

$rules = array('notEmpty' => array('rule' => 'notEmpty'));
$Field->setRules($rules, true);
$result = $Field->getRules();
$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
}

/**
Expand Down

0 comments on commit ac087ec

Please sign in to comment.