Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix rules set being updated with array instead of CakeValidationRule …

…objects. Closes #3367
  • Loading branch information...
commit ac087ec9385899447d5bb0773d37601f0984b24b 1 parent e0586da
@ADmad ADmad authored
View
9 lib/Cake/Model/Validator/CakeValidationSet.php
@@ -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;
}
View
12 lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php
@@ -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);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.