mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Implemented add and remove in ModelValidator
This commit is contained in:
parent
a7222bccd6
commit
d4511af27b
2 changed files with 81 additions and 0 deletions
|
@ -491,4 +491,38 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
|||
return count($this->_fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new rule to a field's rule set
|
||||
*
|
||||
* @param string $field The name of the field from wich the rule will be removed
|
||||
* @param array|CakeRule $rule the rule to be added to the field's rule set
|
||||
* @return ModelValidator this instance
|
||||
**/
|
||||
public function add($field, $name, $rule) {
|
||||
$this->_parseRules();
|
||||
if (!isset($this->_fields[$field])) {
|
||||
$rule = array($name => $rule);
|
||||
$this->_fields[$field] = new CakeValidationSet($field, $rule, $this->getMethods());
|
||||
} else {
|
||||
$this->_fields[$field]->setRule($name, $rule);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a rule from the set by its name
|
||||
*
|
||||
* @param string $field The name of the field from wich the rule will be removed
|
||||
* @param string $rule the name of the rule to be removed
|
||||
* @return ModelValidator this instance
|
||||
**/
|
||||
public function remove($field, $rule = null) {
|
||||
$this->_parseRules();
|
||||
if ($rule === null) {
|
||||
unset($this->_fields[$field]);
|
||||
} else {
|
||||
$this->_fields[$field]->removeRule($rule);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1833,4 +1833,51 @@ class ModelValidationTest extends BaseModelTest {
|
|||
$this->assertCount(2, $Validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests it is possible to add validation rules
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRule() {
|
||||
$TestModel = new Article();
|
||||
$Validator = $TestModel->validator();
|
||||
|
||||
$set = array(
|
||||
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
|
||||
'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
|
||||
);
|
||||
|
||||
$Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
|
||||
$Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
|
||||
$rules = $Validator['other'];
|
||||
$this->assertEquals('other', $rules->field);
|
||||
|
||||
$validators = $rules->getRules();
|
||||
$this->assertCount(2, $validators);
|
||||
$this->assertEquals('numeric', $validators['numeric']->rule);
|
||||
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests it is possible to remove validation rules
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveRule() {
|
||||
$TestModel = new Article();
|
||||
$Validator = $TestModel->validator();
|
||||
|
||||
$this->assertTrue(isset($Validator['title']));
|
||||
$Validator->remove('title');
|
||||
$this->assertFalse(isset($Validator['title']));
|
||||
|
||||
$Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
|
||||
$Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
|
||||
$this->assertTrue(isset($Validator['other']));
|
||||
|
||||
$Validator->remove('other', 'numeric');
|
||||
$this->assertTrue(isset($Validator['other']));
|
||||
$this->assertFalse(isset($Validator['other']['numeric']));
|
||||
$this->assertTrue(isset($Validator['other']['range']));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue