diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index 75a145589..e6f9deeaf 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -331,7 +331,8 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $this->_fields = array(); $methods = $this->getMethods(); foreach ($this->_validate as $fieldName => $ruleSet) { - $this->_fields[$fieldName] = new CakeValidationSet($fieldName, $ruleSet, $methods); + $this->_fields[$fieldName] = new CakeValidationSet($fieldName, $ruleSet); + $this->_fields[$fieldName]->setMethods($methods); } return true; } @@ -483,7 +484,9 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { public function offsetSet($field, $rules) { $this->_parseRules(); if (!$rules instanceof CakeValidationSet) { - $rules = new CakeValidationSet($field, $rules, $this->getMethods()); + $rules = new CakeValidationSet($field, $rules); + $methods = $this->getMethods(); + $rules->setMethods($methods); } $this->_fields[$field] = $rules; } @@ -551,7 +554,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { if (!isset($this->_fields[$field])) { $rule = (is_string($name)) ? array($name => $rule) : $name; - $this->_fields[$field] = new CakeValidationSet($field, $rule, $this->getMethods()); + $this->_fields[$field] = new CakeValidationSet($field, $rule); } else { if (is_string($name)) { $this->_fields[$field]->setRule($name, $rule); @@ -559,6 +562,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { $this->_fields[$field]->setRules($name); } } + + $methods = $this->getMethods(); + $this->_fields[$field]->setMethods($methods); + return $this; } diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index fcfd877f2..e7a2f3c3d 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -2106,4 +2106,28 @@ class ModelValidationTest extends BaseModelTest { $this->assertEquals('awesome', $rules['isAwesome']->rule); } +/** + * Test to ensure custom validation methods work with CakeValidationSet + * + * @return void + */ + public function testCustomMethodsWithCakeValidationSet() { + $TestModel = new TestValidate(); + $Validator = $TestModel->validator(); + + $Validator->add('title', 'validateTitle', array( + 'rule' => 'validateTitle', + 'message' => 'That aint right', + )); + $data = array('title' => 'notatitle'); + $result = $Validator->getField('title')->validate($data); + $expected = array(0 => 'That aint right'); + $this->assertEquals($expected, $result); + + $data = array('title' => 'title-is-good'); + $result = $Validator->getField('title')->validate($data); + $expected = array(); + $this->assertEquals($expected, $result); + } + }