Permalink
Browse files

Fix rules set being updated with array instead of CakeValidationRule …

…objects. Closes #3367
  • Loading branch information...
1 parent e0586da commit ac087ec9385899447d5bb0773d37601f0984b24b @ADmad ADmad committed Nov 13, 2012
@@ -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;
@@ -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;
}
@@ -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);
}
/**

0 comments on commit ac087ec

Please sign in to comment.