Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix custom validation methods with CakeValidationSet

  • Loading branch information...
commit 54745aada9d20791bd8c1b2fd00b795af72a3791 1 parent fddcdd6
@shama shama authored
View
13 lib/Cake/Model/ModelValidator.php
@@ -331,7 +331,8 @@ protected function _parseRules() {
$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 @@ public function offsetGet($field) {
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 @@ public function add($field, $name, $rule = null) {
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 @@ public function add($field, $name, $rule = null) {
$this->_fields[$field]->setRules($name);
}
}
+
+ $methods = $this->getMethods();
+ $this->_fields[$field]->setMethods($methods);
+
return $this;
}
View
24 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -2106,4 +2106,28 @@ public function testDynamicValidationRuleBuilding() {
$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);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.