Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improving ModelValidator::add() to allow multiple rules to be defined…

… at once
  • Loading branch information...
commit 9cceb1553f344442789462c1fe7f67683b7474d8 1 parent cfd9d8a
José Lorenzo Rodríguez lorenzo authored
27 lib/Cake/Model/ModelValidator.php
View
@@ -518,7 +518,9 @@ public function count() {
}
/**
- * 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 @@ public function count() {
* $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;
}
32 lib/Cake/Test/Case/Model/ModelValidationTest.php
View
@@ -1847,11 +1847,6 @@ 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'];
@@ -2035,4 +2030,31 @@ public function testFirstWithDefaults() {
$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'));
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.