Fix custom validation methods with CakeValidationSet

This commit is contained in:
Kyle Robinson Young 2012-06-11 23:24:14 -07:00
parent fddcdd622d
commit 54745aada9
2 changed files with 34 additions and 3 deletions

View file

@ -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;
}

View file

@ -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);
}
}