2012-03-22 11:18:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelValidator.
|
|
|
|
*
|
|
|
|
* Provides the Model validation logic.
|
|
|
|
*
|
|
|
|
* PHP versions 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2012-04-23 11:02:26 +00:00
|
|
|
* @package Cake.Model.Validator
|
|
|
|
* @since CakePHP(tm) v 2.2.0
|
2012-03-22 11:18:57 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
App::uses('ModelValidator', 'Model');
|
|
|
|
App::uses('CakeRule', 'Model/Validator');
|
|
|
|
|
|
|
|
/**
|
2012-04-23 11:02:26 +00:00
|
|
|
* CakeField object.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
2012-04-23 11:02:26 +00:00
|
|
|
* @package Cake.Model.Validator
|
2012-03-22 11:18:57 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/data-validation.html
|
|
|
|
*/
|
|
|
|
class CakeField {
|
|
|
|
|
|
|
|
/**
|
2012-04-29 03:55:05 +00:00
|
|
|
* Holds the ValidationRule objects
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-04-29 03:55:05 +00:00
|
|
|
* @var array
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
protected $_rules = array();
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
/**
|
2012-04-29 03:55:05 +00:00
|
|
|
* Set of methods available for validation
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var array
|
2012-04-29 03:55:05 +00:00
|
|
|
**/
|
|
|
|
protected $_methods = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I18n domain for validation messages.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
**/
|
|
|
|
protected $_validationDomain = null;
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the validation is stopped
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $isStopped = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the fieldname
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $field = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the original ruleSet
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $ruleSet = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param string $fieldName The fieldname
|
2012-04-29 03:55:05 +00:00
|
|
|
* @param array $ruleset
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
public function __construct($fieldName, $ruleSet) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->field = $fieldName;
|
|
|
|
|
|
|
|
if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
|
|
|
|
$ruleSet = array($ruleSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ruleSet as $index => $validateProp) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$this->_rules[$index] = new CakeRule($this->field, $validateProp, $index);
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
$this->ruleSet = $ruleSet;
|
2012-04-29 03:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the list of methods to use for validation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
public function setMethods(&$methods) {
|
|
|
|
$this->_methods =& $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the I18n domain for validation messages.
|
|
|
|
*
|
|
|
|
* @param string $validationDomain The validation domain to be used.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setValidationDomain($validationDomain) {
|
|
|
|
$this->_validationDomain = $validationDomain;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a ModelField
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-04-29 03:55:05 +00:00
|
|
|
* @return array list of validation errors for this field
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
public function validate($data, $isUpdate = false) {
|
|
|
|
$errors = array();
|
2012-03-22 11:18:57 +00:00
|
|
|
foreach ($this->getRules() as $rule) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$rule->isUpdate($isUpdate);
|
2012-03-22 11:18:57 +00:00
|
|
|
if ($rule->skip()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
$checkRequired = $rule->checkRequired($data);
|
|
|
|
if (!$checkRequired && array_key_exists($this->field, $data)) {
|
|
|
|
if ($rule->checkEmpty($data)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-04-29 03:55:05 +00:00
|
|
|
$rule->dispatchValidation($data, $this->_methods);
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
if ($checkRequired || !$rule->isValid($data)) {
|
|
|
|
$errors[] = $this->_processValidationResponse($rule);
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
if ($rule->isLast()) {
|
2012-04-29 03:55:05 +00:00
|
|
|
break;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
return $errors;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a rule for a certain index
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param mixed index
|
|
|
|
* @return ValidationRule
|
|
|
|
*/
|
|
|
|
public function getRule($index) {
|
|
|
|
if (!empty($this->_rules[$index])) {
|
|
|
|
return $this->_rules[$index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all rules for this ModelField
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRules() {
|
|
|
|
return $this->_rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a ValidationRule $rule for key $key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param mixed $key The key under which the rule should be set
|
|
|
|
* @param ValidationRule $rule The ValidationRule to be set
|
|
|
|
* @return ModelField
|
|
|
|
*/
|
|
|
|
public function setRule($key, CakeRule $rule) {
|
|
|
|
$this->_rules[$key] = $rule;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the rules for a given field
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param array $rules The rules to be set
|
|
|
|
* @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
|
|
|
|
* @return ModelField
|
|
|
|
*/
|
|
|
|
public function setRules($rules = array(), $mergeVars = true) {
|
|
|
|
if ($mergeVars === false) {
|
|
|
|
$this->_rules = $rules;
|
|
|
|
} else {
|
|
|
|
$this->_rules = array_merge($this->_rules, $rules);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-29 03:55:05 +00:00
|
|
|
* Fetches the correct error message for a failed validation
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-04-29 03:55:05 +00:00
|
|
|
* @return string
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
protected function _processValidationResponse($rule) {
|
|
|
|
$message = $rule->getValidationResult();
|
|
|
|
$name = $rule->getName();
|
|
|
|
if (is_string($message)) {
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
$message = $rule->message;
|
|
|
|
|
|
|
|
if ($message !== null && !is_string($message)) {
|
|
|
|
$args = null;
|
|
|
|
if (is_array($message)) {
|
|
|
|
$result = $message[0];
|
|
|
|
$args = array_slice($message, 1);
|
|
|
|
} else {
|
|
|
|
$result = $message;
|
|
|
|
}
|
|
|
|
if (is_array($rule->rule) && $args === null) {
|
|
|
|
$args = array_slice($rule->rule, 1);
|
|
|
|
}
|
|
|
|
if (!empty($args)) {
|
|
|
|
foreach ($args as $k => $arg) {
|
|
|
|
$args[$k] = __d($this->_validationDomain, $arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$message = __d($this->_validationDomain, $result, $args);
|
|
|
|
} elseif (is_string($name)) {
|
|
|
|
if (is_array($rule->rule)) {
|
|
|
|
$args = array_slice($rule->rule, 1);
|
|
|
|
if (!empty($args)) {
|
|
|
|
foreach ($args as $k => $arg) {
|
|
|
|
$args[$k] = __d($this->_validationDomain, $arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$message = __d($this->_validationDomain, $name, $args);
|
|
|
|
} else {
|
|
|
|
$message = __d($this->_validationDomain, $name);
|
|
|
|
}
|
|
|
|
//} elseif (!$rule->checkRequired() && is_numeric($name) && count($this->ruleSet) > 1) {
|
|
|
|
// $this->_errorMessage = $this->_index + 1;
|
|
|
|
} else {
|
|
|
|
$message = __d('cake_dev', 'This field cannot be left blank');
|
|
|
|
}
|
2012-03-22 11:18:57 +00:00
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
return $message;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|