Skip to content

Commit

Permalink
Implemented add and remove in ModelValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 7, 2012
1 parent a7222bc commit d4511af
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/Cake/Model/ModelValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
47 changes: 47 additions & 0 deletions lib/Cake/Test/Case/Model/ModelValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.