/** * {@inheritdoc} */ public function beforeValidate(Model $Model, $options = array()) { $ModelValidator = $Model->validator(); foreach ($Model->data[$Model->alias] as $field => $value) { if (!preg_match('/^([a-z0-9_]+)_confirm$/i', $field, $match)) { continue; } if (!array_key_exists($match[1], $Model->data[$Model->alias])) { continue; } if (!($Ruleset = $ModelValidator->getField($match[1]))) { $Ruleset = new CakeValidationSet($match[1], array()); } $ruleset = array(); foreach ($Ruleset->getRules() as $name => $Rule) { $ruleset[$name] = (array) $Rule; foreach (array_keys($ruleset[$name]) as $key) { if (!preg_match('/^[a-z]/i', $key)) { unset($ruleset[$name][$key]); } } } $ModelValidator->add($field, new CakeValidationSet($field, array())); $ModelValidator->getField($field)->setRule('confirmed', array('rule' => 'isConfirmed', 'message' => __d('common', "No match."))); } return true; }
protected function _validateCollection($data, $ruleset) { foreach ($data as $field => $value) { if (is_array($value) && isset($ruleset["_{$field}"])) { $status &= $this->_validateCollection($data[$field], $ruleset["_{$field}"]); continue; } if (!isset($ruleset[$field])) { continue; } $obj = new CakeValidationSet($field, $ruleset[$field]); $obj->setMethods($this->_methods[$this->_Model->name]); $errors = $obj->validate($data); foreach ($errors as $error) { $this->_Model->invalidate($field, $error); } } }
/** * Sets the rule set for a field * * @param string $field name of the field to set * @param array|CakeValidationSet $rules set of rules to apply to field * @return void */ public function offsetSet($field, $rules) { $this->_parseRules(); if (!$rules instanceof CakeValidationSet) { $rules = new CakeValidationSet($field, $rules); $methods = $this->getMethods(); $rules->setMethods($methods); } $this->_fields[$field] = $rules; }
/** * Test removeRule method * * @return void */ public function testRemoveRule() { $Set = new CakeValidationSet('title', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true), 'numeric' => array('rule' => 'numeric'), 'other' => array('rule' => array('other', 1)))); $Set->removeRule('notEmpty'); $this->assertFalse(isset($Set['notEmpty'])); $Set->removeRule('numeric'); $this->assertFalse(isset($Set['numeric'])); $Set->removeRule('other'); $this->assertFalse(isset($Set['other'])); }