Fix rules set being updated with array instead of CakeValidationRule objects. Closes #3367

This commit is contained in:
ADmad 2012-11-13 22:13:15 +05:30
parent e0586da808
commit ac087ec938
2 changed files with 17 additions and 4 deletions

View file

@ -192,7 +192,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
* @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;
@ -236,9 +236,10 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
*/
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;
}

View file

@ -156,10 +156,22 @@ class CakeValidationSetTest extends CakeTestCase {
$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);
}
/**