From 9cceb1553f344442789462c1fe7f67683b7474d8 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 20 May 2012 18:45:46 -0430 Subject: [PATCH] Improving ModelValidator::add() to allow multiple rules to be defined at once --- lib/Cake/Model/ModelValidator.php | 27 +++++++++++++--- .../Test/Case/Model/ModelValidationTest.php | 32 ++++++++++++++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index 49b0b5a24..3908e40e1 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -518,7 +518,9 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { } /** - * Adds a new rule to a field's rule set + * Adds a new rule to a field's rule set. If second argumet is an array or instance of + * CakeValidationSet then rules list for the field will be replaced with second argument and + * third argument will be ignored. * * ## Example: * @@ -526,19 +528,34 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { * $validator * ->add('title', 'required', array('rule' => 'notEmpty', 'required' => true)) * ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User')) + * + * $validator->add('password', array( + * 'size' => array('rule' => array('between', 8, 20)), + * 'hasSpecialCharacter' => array('rule' => 'validateSpecialchar', 'message' => 'not valid') + * )); * }}} * * @param string $field The name of the field from wich the rule will be removed - * @param array|CakeValidationRule $rule the rule to be added to the field's rule set + * @param string|array|CakeValidationSet $name name of the rule to be added or list of rules for the field + * @param array|CakeValidationRule $rule or list of rules to be added to the field's rule set * @return ModelValidator this instance **/ - public function add($field, $name, $rule) { + public function add($field, $name, $rule = null) { $this->_parseRules(); + if ($name instanceof CakeValidationSet) { + $this->_fields[$field] = $name; + return $this; + } + if (!isset($this->_fields[$field])) { - $rule = array($name => $rule); + $rule = (is_string($name)) ? array($name => $rule) : $name; $this->_fields[$field] = new CakeValidationSet($field, $rule, $this->getMethods()); } else { - $this->_fields[$field]->setRule($name, $rule); + if (is_string($name)) { + $this->_fields[$field]->setRule($name, $rule); + } else { + $this->_fields[$field]->setRules($name); + } } return $this; } diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 04c0c2d3c..d54de7b02 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1847,11 +1847,6 @@ class ModelValidationTest extends BaseModelTest { $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']; @@ -2035,4 +2030,31 @@ class ModelValidationTest extends BaseModelTest { $this->assertEquals($expected['Article'], $result['Article']); } + public function testAddMultipleRules() { + $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', $set); + $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); + + $set = new CakeValidationSet('other', array( + 'a' => array('rule' => 'numeric', 'allowEmpty' => false), + 'b' => array('rule' => array('between', 1, 5), 'allowEmpty' => false), + )); + + $Validator->add('other', $set); + $this->assertSame($set, $Validator->getField('other')); + } + }