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

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-20 18:45:46 -04:30
parent cfd9d8a815
commit 9cceb1553f
2 changed files with 49 additions and 10 deletions

View file

@ -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: * ## Example:
* *
@ -526,19 +528,34 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* $validator * $validator
* ->add('title', 'required', array('rule' => 'notEmpty', 'required' => true)) * ->add('title', 'required', array('rule' => 'notEmpty', 'required' => true))
* ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User')) * ->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 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 * @return ModelValidator this instance
**/ **/
public function add($field, $name, $rule) { public function add($field, $name, $rule = null) {
$this->_parseRules(); $this->_parseRules();
if ($name instanceof CakeValidationSet) {
$this->_fields[$field] = $name;
return $this;
}
if (!isset($this->_fields[$field])) { 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()); $this->_fields[$field] = new CakeValidationSet($field, $rule, $this->getMethods());
} else { } 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; return $this;
} }

View file

@ -1847,11 +1847,6 @@ class ModelValidationTest extends BaseModelTest {
$TestModel = new Article(); $TestModel = new Article();
$Validator = $TestModel->validator(); $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', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
$Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false)); $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
$rules = $Validator['other']; $rules = $Validator['other'];
@ -2035,4 +2030,31 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals($expected['Article'], $result['Article']); $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'));
}
} }