mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 11:06:15 +00:00
Merge pull request #692 from shama/patch-custom-validation
Fix custom validation methods with CakeValidationSet
This commit is contained in:
commit
5f0e0963db
2 changed files with 34 additions and 3 deletions
|
@ -331,7 +331,8 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
||||||
$this->_fields = array();
|
$this->_fields = array();
|
||||||
$methods = $this->getMethods();
|
$methods = $this->getMethods();
|
||||||
foreach ($this->_validate as $fieldName => $ruleSet) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -483,7 +484,9 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
||||||
public function offsetSet($field, $rules) {
|
public function offsetSet($field, $rules) {
|
||||||
$this->_parseRules();
|
$this->_parseRules();
|
||||||
if (!$rules instanceof CakeValidationSet) {
|
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;
|
$this->_fields[$field] = $rules;
|
||||||
}
|
}
|
||||||
|
@ -551,7 +554,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
||||||
|
|
||||||
if (!isset($this->_fields[$field])) {
|
if (!isset($this->_fields[$field])) {
|
||||||
$rule = (is_string($name)) ? array($name => $rule) : $name;
|
$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 {
|
} else {
|
||||||
if (is_string($name)) {
|
if (is_string($name)) {
|
||||||
$this->_fields[$field]->setRule($name, $rule);
|
$this->_fields[$field]->setRule($name, $rule);
|
||||||
|
@ -559,6 +562,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
||||||
$this->_fields[$field]->setRules($name);
|
$this->_fields[$field]->setRules($name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$methods = $this->getMethods();
|
||||||
|
$this->_fields[$field]->setMethods($methods);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2106,4 +2106,28 @@ class ModelValidationTest extends BaseModelTest {
|
||||||
$this->assertEquals('awesome', $rules['isAwesome']->rule);
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue