diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index 5460f81a8..ba84d3b67 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -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; + } } diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 5ff65c0c6..9adff1c80 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -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'])); + } }