Skip to content
Permalink
Browse files

Implemented add and remove in ModelValidator

  • Loading branch information...
lorenzo committed May 7, 2012
1 parent a7222bc commit d4511af27b60ba3d8d3040bdc1ffb34e29093a3e
Showing with 81 additions and 0 deletions.
  1. +34 −0 lib/Cake/Model/ModelValidator.php
  2. +47 −0 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -491,4 +491,38 @@ public function count() {
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 @@ public function testCount() {
$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']));
}
}

0 comments on commit d4511af

Please sign in to comment.
You can’t perform that action at this time.