| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?php
- /**
- * CakeRule.
- *
- * 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
- * @package Cake.Model.Validator
- * @since CakePHP(tm) v 2.2.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('ModelValidator', 'Model');
- App::uses('CakeField', 'Model/Validator');
- App::uses('Validation', 'Utility');
- /**
- * CakeRule object.
- *
- * @package Cake.Model.Validator
- * @link http://book.cakephp.org/2.0/en/data-validation.html
- */
- class CakeRule {
- /**
- * Holds a reference to the parent field
- *
- * @var CakeField
- */
- protected $_field = null;
- /**
- * Has the required check failed?
- *
- * @var boolean
- */
- protected $_requiredFail = null;
- /**
- * The 'valid' value
- *
- * @var mixed
- */
- protected $_valid = true;
- /**
- * Holds the index under which the Validator was attached
- *
- * @var mixed
- */
- protected $_index = null;
- /**
- * Create or Update transaction?
- *
- * @var boolean
- */
- protected $_modelExists = null;
- /**
- * The parsed rule
- *
- * @var mixed
- */
- protected $_rule = null;
- /**
- * The parsed rule parameters
- *
- * @var array
- */
- protected $_ruleParams = array();
- /**
- * The errorMessage
- *
- * @var string
- */
- protected $_errorMessage = null;
- /**
- * Holds passed in options
- *
- * @var array
- */
- protected $_passedOptions = array();
- /**
- * Flag indicating wether the allowEmpty check has failed
- *
- * @var boolean
- */
- protected $_emptyFail = null;
- /**
- * The 'rule' key
- *
- * @var mixed
- */
- public $rule = 'blank';
- /**
- * The 'required' key
- *
- * @var mixed
- */
- public $required = null;
- /**
- * The 'allowEmpty' key
- *
- * @var boolean
- */
- public $allowEmpty = false;
- /**
- * The 'on' key
- *
- * @var string
- */
- public $on = null;
- /**
- * The 'last' key
- *
- * @var boolean
- */
- public $last = true;
- /**
- * The 'message' key
- *
- * @var string
- */
- public $message = null;
- /**
- * Constructor
- *
- * @param CakeField $field
- * @param array $validator [optional] The validator properties
- * @param mixed $index [optional]
- */
- public function __construct(CakeField $field, $validator = array(), $index = null) {
- $this->_field = $field;
- $this->_index = $index;
- unset($field, $index);
- $this->data = &$this->getField()
- ->data;
- $this->_modelExists = $this->getField()
- ->getValidator()
- ->getModel()
- ->exists();
- $this->_addValidatorProps($validator);
- unset($validator);
- }
- /**
- * Checks if the rule is valid
- *
- * @return boolean
- */
- public function isValid() {
- if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
- return false;
- }
- return true;
- }
- /**
- * Checks if the field is required by the 'required' value
- *
- * @return boolean
- */
- public function isRequired() {
- if (is_bool($this->required)) {
- return $this->required;
- }
- if (in_array($this->required, array('create', 'update'), true)) {
- if ($this->required === 'create' && !$this->_modelExists || $this->required === 'update' && $this->_modelExists) {
- $this->required = true;
- }
- }
- return $this->required;
- }
- /**
- * Checks if the field failed the required validation
- *
- * @return boolean
- */
- public function checkRequired() {
- if ($this->_requiredFail !== null) {
- return $this->_requiredFail;
- }
- $this->_requiredFail = (
- (!isset($this->data[$this->getField()->field]) && $this->required === true) ||
- (
- isset($this->data[$this->getField()->field]) && (empty($this->data[$this->getField()->field]) &&
- !is_numeric($this->data[$this->getField()->field])) && $this->allowEmpty === false
- )
- );
- return $this->_requiredFail;
- }
- /**
- * Checks if the allowEmpty key applies
- *
- * @return boolean
- */
- public function checkEmpty() {
- if ($this->_emptyFail !== null) {
- return $this->_emptyFail;
- }
- $this->_emptyFail = false;
- if (empty($this->data[$this->getField()->field]) && $this->data[$this->getField()->field] != '0' && $this->allowEmpty === true) {
- $this->_emptyFail = true;
- }
- return $this->_emptyFail;
- }
- /**
- * Checks if the Validation rule can be skipped
- *
- * @return boolean True if the ValidationRule can be skipped
- */
- public function skip() {
- if (!empty($this->on)) {
- if ($this->on == 'create' && $this->_modelExists || $this->on == 'update' && !$this->_modelExists) {
- return true;
- }
- }
- return false;
- }
- /**
- * Checks if the 'last' key is true
- *
- * @return boolean
- */
- public function isLast() {
- return (bool) $this->last;
- }
- /**
- * Gets the validation error message
- *
- * @return string
- */
- public function getMessage() {
- return $this->_processValidationResponse();
- }
- /**
- * Gets the parent field
- *
- * @return CakeField
- */
- public function getField() {
- return $this->_field;
- }
- /**
- * Gets an array with the rule properties
- *
- * @return array
- */
- public function getPropertiesArray() {
- return array(
- 'rule' => $this->rule,
- 'required' => $this->required,
- 'allowEmpty' => $this->allowEmpty,
- 'on' => $this->on,
- 'last' => $this->last,
- 'message' => $this->message
- );
- }
- /**
- * Dispatches the validation rule to the given validator method
- *
- * @return boolean True if the rule could be dispatched, false otherwise
- */
- public function dispatchValidation() {
- $this->_parseRule();
- $validator = $this->getPropertiesArray();
- $methods = $this->getField()->getValidator()->getMethods();
- $Model = $this->getField()->getValidator()->getModel();
- if (in_array(strtolower($this->_rule), $methods['model'])) {
- $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
- $this->_ruleParams[0] = array($this->getField()->field => $this->_ruleParams[0]);
- $this->_valid = $Model->dispatchMethod($this->_rule, $this->_ruleParams);
- } elseif (in_array($this->_rule, $methods['behaviors']) || in_array(strtolower($this->_rule), $methods['behaviors'])) {
- $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
- $this->_ruleParams[0] = array($this->getField()->field => $this->_ruleParams[0]);
- $this->_valid = $Model->Behaviors->dispatchMethod($Model, $this->_rule, $this->_ruleParams);
- } elseif (method_exists('Validation', $this->_rule)) {
- $this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
- } elseif (!is_array($validator['rule'])) {
- $this->_valid = preg_match($this->_rule, $this->data[$this->getField()->field]);
- } elseif (Configure::read('debug') > 0) {
- trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $this->_field->field), E_USER_WARNING);
- return false;
- }
- unset($validator, $methods, $Model);
- return true;
- }
- /**
- * Fetches the correct error message for a failed validation
- *
- * @return string
- */
- protected function _processValidationResponse() {
- $validationDomain = $this->_field->getValidator()->validationDomain;
- if (is_string($this->_valid)) {
- $this->_errorMessage = $this->_valid;
- } elseif ($this->message !== null) {
- $args = null;
- if (is_array($this->message)) {
- $this->_errorMessage = $this->message[0];
- $args = array_slice($this->message, 1);
- } else {
- $this->_errorMessage = $this->message;
- }
- if (is_array($this->rule) && $args === null) {
- $args = array_slice($this->getField()->ruleSet[$this->_index]['rule'], 1);
- }
- if (!empty($args)) {
- foreach ($args as $k => $arg) {
- $args[$k] = __d($validationDomain, $arg);
- }
- }
- $this->_errorMessage = __d($validationDomain, $this->_errorMessage, $args);
- } elseif (is_string($this->_index)) {
- if (is_array($this->rule)) {
- $args = array_slice($this->getField()->ruleSet[$this->_index]['rule'], 1);
- if (!empty($args)) {
- foreach ($args as $k => $arg) {
- $args[$k] = __d($validationDomain, $arg);
- }
- }
- $this->_errorMessage = __d($validationDomain, $this->_index, $args);
- } else {
- $this->_errorMessage = __d($validationDomain, $this->_index);
- }
- } elseif (!$this->checkRequired() && is_numeric($this->_index) && count($this->getField()->ruleSet) > 1) {
- $this->_errorMessage = $this->_index + 1;
- } else {
- $this->_errorMessage = __d('cake_dev', 'This field cannot be left blank');
- }
- unset($validationDomain);
- return $this->_errorMessage;
- }
- /**
- * Sets the rule properties from the rule entry in validate
- *
- * @param array $validator [optional]
- * @return void
- */
- protected function _addValidatorProps($validator = array()) {
- if (!is_array($validator)) {
- $validator = array('rule' => $validator);
- }
- foreach ($validator as $key => $value) {
- if (isset($value) || !empty($value)) {
- if (in_array($key, array('rule', 'required', 'allowEmpty', 'on', 'message', 'last'))) {
- $this->$key = $validator[$key];
- } else {
- $this->_passedOptions[$key] = $value;
- }
- }
- }
- unset($validator);
- }
- /**
- * Parses the rule and sets the rule and ruleParams
- *
- * @return void
- */
- protected function _parseRule() {
- if (is_array($this->rule)) {
- $this->_rule = $this->rule[0];
- unset($this->rule[0]);
- $this->_ruleParams = array_merge(array($this->data[$this->getField()->field]), array_values($this->rule));
- } else {
- $this->_rule = $this->rule;
- $this->_ruleParams = array($this->data[$this->getField()->field]);
- }
- }
- }
|