function _check() { $_this =& Validation::getInstance(); if (preg_match($_this->regex, $_this->subject)) { $_this->error[] = false; return true; } else { $_this->error[] = true; return false; } }
/** * Returns an array of fields that have failed validation. * * @param string $options An optional array of custom options to be made available in the beforeValidate callback * @return array Array of invalid fields * @access public * @link http://book.cakephp.org/view/410/Validating-Data-from-the-Controller */ function invalidFields($options = array()) { if (!$this->Behaviors->trigger($this, 'beforeValidate', array($options), array('break' => true, 'breakOn' => false)) || $this->beforeValidate($options) === false) { return $this->validationErrors; } if (!isset($this->validate) || empty($this->validate)) { return $this->validationErrors; } $data = $this->data; $methods = array_map('strtolower', get_class_methods($this)); $behaviorMethods = array_keys($this->Behaviors->methods()); if (isset($data[$this->alias])) { $data = $data[$this->alias]; } elseif (!is_array($data)) { $data = array(); } $Validation =& Validation::getInstance(); $this->exists(); $_validate = $this->validate; if (array_key_exists('fieldList', $options) && is_array($options['fieldList']) && !empty($options['fieldList'])) { $validate = array(); foreach ($options['fieldList'] as $f) { if (!empty($this->validate[$f])) { $validate[$f] = $this->validate[$f]; } } $this->validate = $validate; } foreach ($this->validate as $fieldName => $ruleSet) { if (!is_array($ruleSet) || is_array($ruleSet) && isset($ruleSet['rule'])) { $ruleSet = array($ruleSet); } $default = array('allowEmpty' => null, 'required' => null, 'rule' => 'blank', 'last' => false, 'on' => null); foreach ($ruleSet as $index => $validator) { if (!is_array($validator)) { $validator = array('rule' => $validator); } $validator = array_merge($default, $validator); if (isset($validator['message'])) { $message = $validator['message']; } else { $message = __('This field cannot be left blank', true); } if (empty($validator['on']) || $validator['on'] == 'create' && !$this->__exists || $validator['on'] == 'update' && $this->__exists) { $required = !isset($data[$fieldName]) && $validator['required'] === true || isset($data[$fieldName]) && (empty($data[$fieldName]) && !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false; if ($required) { $this->invalidate($fieldName, $message); if ($validator['last']) { break; } } elseif (array_key_exists($fieldName, $data)) { if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) { break; } if (is_array($validator['rule'])) { $rule = $validator['rule'][0]; unset($validator['rule'][0]); $ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule'])); } else { $rule = $validator['rule']; $ruleParams = array($data[$fieldName]); } $valid = true; if (in_array(strtolower($rule), $methods)) { $ruleParams[] = $validator; $ruleParams[0] = array($fieldName => $ruleParams[0]); $valid = $this->dispatchMethod($rule, $ruleParams); } elseif (in_array($rule, $behaviorMethods) || in_array(strtolower($rule), $behaviorMethods)) { $ruleParams[] = $validator; $ruleParams[0] = array($fieldName => $ruleParams[0]); $valid = $this->Behaviors->dispatchMethod($this, $rule, $ruleParams); } elseif (method_exists($Validation, $rule)) { $valid = $Validation->dispatchMethod($rule, $ruleParams); } elseif (!is_array($validator['rule'])) { $valid = preg_match($rule, $data[$fieldName]); } if (!$valid || is_string($valid) && strlen($valid) > 0) { if (is_string($valid) && strlen($valid) > 0) { $validator['message'] = $valid; } elseif (!isset($validator['message'])) { if (is_string($index)) { $validator['message'] = $index; } elseif (is_numeric($index) && count($ruleSet) > 1) { $validator['message'] = $index + 1; } else { $validator['message'] = $message; } } $this->invalidate($fieldName, $validator['message']); if ($validator['last']) { break; } } } } } } $this->validate = $_validate; return $this->validationErrors; }
/** * setup method * * @access public * @return void */ function setUp() { $this->Validation =& Validation::getInstance(); $this->_appEncoding = Configure::read('App.encoding'); }
/** * Luhn algorithm * * @see http://en.wikipedia.org/wiki/Luhn_algorithm * @return boolean Success * @access protected */ function _luhn() { $_this =& Validation::getInstance(); if ($_this->deep === true) { if ($_this->check == 0) { return false; } $sum = 0; $length = strlen($_this->check); for ($position = 1 - $length % 2; $position < $length; $position += 2) { $sum += substr($_this->check, $position, 1); } for ($position = $length % 2; $position < $length; $position += 2) { $number = substr($_this->check, $position, 1) * 2; if ($number < 10) { $sum += $number; } else { $sum += $number - 9; } } if ($sum % 10 != 0) { return false; } } return true; }
/** * Luhn algorithm * * @see http://en.wikipedia.org/wiki/Luhn_algorithm * @return boolean Success * @access protected */ function _luhn() { $_this =& Validation::getInstance(); if ($_this->deep !== true) { return true; } if ($_this->check == 0) { return false; } $sum = 0; $length = strlen($_this->check); for ($position = 1 - $length % 2; $position < $length; $position += 2) { $sum += $_this->check[$position]; } for ($position = $length % 2; $position < $length; $position += 2) { $number = $_this->check[$position] * 2; $sum += $number < 10 ? $number : $number - 9; } return $sum % 10 == 0; }
function __convertRule($rule) { $regex = false; if ($rule == '_extract') { return false; } if (is_array($this->whitelist) && !in_array($rule, $this->whitelist)) { return false; } $params = array(); if (is_array($rule)) { $params = array_slice($rule, 1); $rule = $rule[0]; } if ($rule == 'comparison') { $params[0] = str_replace(array(' ', "\t", "\n", "\r", "", "\v"), '', strtolower($params[0])); switch ($params[0]) { case 'isgreater': $params[0] = '>'; break; case 'isless': $params[0] = '<'; break; case 'greaterorequal': $params[0] = '>='; break; case 'lessorequal': $params[0] = '<='; break; case 'equalto': $params[0] = '=='; break; case 'notequal': $params[0] = '!='; break; } } //Certain Cake built-in validations can be handled with regular expressions, //but aren't on the Cake side. switch ($rule) { case 'alphaNumeric': return '/^[0-9A-Za-z]+$/'; case 'between': return sprintf('/^.{%d,%d}$/', $params[0], $params[1]); case 'boolean': return array('rule' => 'boolean'); case 'date': //Some of Cake's date regexs aren't JavaScript compatible. Skip for now if (!empty($params[0])) { $params = $params[0]; } else { $params = 'ymd'; } return array('rule' => 'date', 'params' => $params); case 'email': return VALID_EMAIL_JS; case 'equalTo': return sprintf('/^%s$/', $params[0]); case 'extension': if (empty($params[0])) { $params = array('gif', 'jpeg', 'png', 'jpg'); } else { $params = $params[0]; } return sprintf('/\\.(%s)$/', implode('|', $params)); case 'inList': return array('rule' => 'inList', 'params' => $params[0]); case 'ip': return VALID_IP_JS; case 'minLength': return sprintf('/^[\\s\\S]{%d,}$/', $params[0]); case 'maxLength': return sprintf('/^[\\s\\S]{0,%d}$/', $params[0]); case 'money': //The Cake regex for money was giving me issues, even within PHP. Skip for now return array('rule' => 'money'); case 'multiple': $defaults = array('in' => null, 'max' => null, 'min' => null); $params = array_merge($defaults, $params[0]); return array('rule' => 'multiple', 'params' => $params); case 'notEmpty': return array('rule' => 'notEmpty'); case 'numeric': //Cake uses PHP's is_numeric function, which actually accepts a varied input //(both +0123.45e6 and 0xFF are valid) then what is allowed in this regular expression. //99% of people using this validation probably want to restrict to just numbers in standard //decimal notation. Feel free to alter or delete. return '/^[+-]?[0-9|.]+$/'; case 'range': case 'comparison': //Don't think there is a way to do this with a regular expressions, //so we'll handle this with plain old javascript return array('rule' => $rule, 'params' => array($params[0], $params[1])); } //try to lookup the regular expression from //CakePHP's built-in validation rules $Validation =& Validation::getInstance(); if (method_exists($Validation, $rule)) { $Validation->regex = null; call_user_func_array(array(&$Validation, $rule), array_merge(array(true), $params)); if ($Validation->regex) { $regex = $Validation->regex; } } if ($regex) { //special handling switch ($rule) { case 'postal': case 'ssn': //I'm not a regex guru and I have no idea what "\\A\\b" and "\\b\\z" do. //Is it just to match start and end of line? Why not use //"^" and "$" then? Eitherway they don't work in JavaScript. return str_replace(array('\\A\\b', '\\b\\z'), array('^', '$'), $regex); } return $regex; } return array('rule' => $rule, 'params' => $params); }
/** * 複数のEメールチェック(カンマ区切り) * * @param array $check * @return boolean */ function emails($check) { $Validation =& Validation::getInstance(); $emails = array(); if (strpos($check[key($check)], ',') !== false) { $emails = explode(',', $check[key($check)]); } if (!$emails) { $emails = array($check[key($check)]); } $result = true; foreach ($emails as $email) { if (!$Validation->email($email)) { $result = false; } } return $result; }
/** * Get Validation component * * @return Validation */ protected static function validation() { return Validation::getInstance(); }
/** * setup method * * @access public * @return void */ function setup() { $this->Validation =& Validation::getInstance(); }
/** * Reset internal variables for another validation run. * * @access private */ function __reset() { $_this =& Validation::getInstance(); $_this->check = null; $_this->regex = null; $_this->country = null; $_this->deep = null; $_this->type = null; }
function convertRule($rule) { $regex = false; if ($rule == '_extract') { return false; } if (is_array($this->whitelist) && !in_array($rule, $this->whitelist)) { return false; } $params = array(); if (is_array($rule)) { $params = array_slice($rule, 1); $rule = $rule[0]; } //Certain Cake built-in validations can be handled with regular expressions, //but aren't on the Cake side. switch ($rule) { case 'between': return sprintf('/^.{%d,%d}$/', $params[0], $params[1]); case 'date': //Some of Cake's date regexs aren't JavaScript compatible. Skip for now return false; case 'email': return VALID_EMAIL_JS; case 'equalTo': return sprintf('/^%s$/', $params[0]); case 'extension': return sprintf('/\\.(%s)$/', implode('|', explode(',', DEFAULT_VALIDATION_EXTENSIONS))); case 'ip': return VALID_IP_JS; case 'minLength': return sprintf('/^.{%d,}$/', $params[0]); case 'maxLength': return sprintf('/^.{0,%d}$/', $params[0]); case 'money': //The Cake regex for money was giving me issues, even within PHP. Skip for now return false; case 'numeric': //Cake uses PHP's is_numeric function, which actually accepts a varied input //(both +0123.45e6 and 0xFF are valid) then what is allowed in this regular expression. //99% of people using this validation probably want to restrict to just numbers in standard //decimal notation. Feel free to alter or delete. return '/^[+-]?[0-9]+$/'; case 'range': //Don't think there is a way to do this with a regular expressions, //so we'll handle this with plain old javascript return sprintf('range|%s|%s', $params[0], $params[1]); } //try to lookup the regular expression from //CakePHP's built-in validation rules $Validation =& Validation::getInstance(); if (method_exists($Validation, $rule)) { $Validation->regex = null; call_user_func_array(array(&$Validation, $rule), array_merge(array(true), $params)); if ($Validation->regex) { $regex = $Validation->regex; } } //special handling switch ($rule) { case 'postal': case 'ssn': //I'm not a regex guru and I have no idea what "\\A\\b" and "\\b\\z" do. //Is it just to match start and end of line? Why not use //"^" and "$" then? Eitherway they don't work in JavaScript. return str_replace(array('\\A\\b', '\\b\\z'), array('^', '$'), $regex); } return $regex; }
/** * Handle validation of a questionnaire response * * @param mixed $check The data to check for validity * @param mixed $rule The rule to check with * @return mixed true if the data is valid, false otherwise * */ function response($check, $rule) { // $check array is passed using the form field name as the key // have to extract the value to make the function generic $value = current(array_values($check)); if (array_key_exists('answer', $value)) { $value = $value['answer']; } else { $value = $value['answer_id']; } $Validation =& Validation::getInstance(); if (method_exists($Validation, $rule)) { return $Validation->dispatchMethod($rule, array($value)); } elseif (!is_array($rule)) { return preg_match($rule, $value); } elseif (Configure::read('debug') > 0) { trigger_error(sprintf(__('Could not find validation handler %s for %s', true), $rule, 'response'), E_USER_WARNING); } return false; }
/** * Returns an array of fields that do not meet validation. * * @param array $data Parameter usage is deprecated, set Model::$data instead * @return array Array of invalid fields * @access public */ function invalidFields($data = array()) { if (!empty($this->behaviors)) { $behaviors = array_keys($this->behaviors); $ct = count($behaviors); for ($i = 0; $i < $ct; $i++) { if ($this->behaviors[$behaviors[$i]]->beforeValidate($this) === false) { return $this->validationErrors; } } } if (!$this->beforeValidate()) { return $this->validationErrors; } if (empty($data)) { $data = $this->data; } else { trigger_error(__('(Model::invalidFields) Parameter usage is deprecated, set the $data property instead', true), E_USER_WARNING); } if (!isset($this->validate) || empty($this->validate)) { return $this->validationErrors; } if (isset($data[$this->alias])) { $data = $data[$this->alias]; } $Validation =& Validation::getInstance(); $exists = $this->exists(); foreach ($this->validate as $fieldName => $ruleSet) { if (!is_array($ruleSet) || is_array($ruleSet) && isset($ruleSet['rule'])) { $ruleSet = array($ruleSet); } foreach ($ruleSet as $index => $validator) { if (!is_array($validator)) { $validator = array('rule' => $validator); } $default = array('allowEmpty' => null, 'required' => null, 'rule' => 'blank', 'last' => false, 'on' => null); $validator = array_merge($default, $validator); if (isset($validator['message'])) { $message = $validator['message']; } else { $message = __('This field cannot be left blank', true); } if (empty($validator['on']) || $validator['on'] == 'create' && !$exists || $validator['on'] == 'update' && $exists) { if (!isset($data[$fieldName]) && $validator['required'] === true || isset($data[$fieldName]) && (empty($data[$fieldName]) && !is_numeric($data[$fieldName])) && $validator['allowEmpty'] === false) { $this->invalidate($fieldName, $message); if ($validator['last']) { break; } } elseif (array_key_exists($fieldName, $data)) { if (empty($data[$fieldName]) && $data[$fieldName] != '0' && $validator['allowEmpty'] === true) { break; } if (is_array($validator['rule'])) { $rule = $validator['rule'][0]; unset($validator['rule'][0]); $ruleParams = array_merge(array($data[$fieldName]), array_values($validator['rule'])); } else { $rule = $validator['rule']; $ruleParams = array($data[$fieldName]); } $valid = true; $msg = null; if (method_exists($this, $rule) || isset($this->__behaviorMethods[$rule]) || isset($this->__behaviorMethods[strtolower($rule)])) { $ruleParams[] = array_diff_key($validator, $default); $ruleParams[0] = array($fieldName => $ruleParams[0]); $valid = call_user_func_array(array(&$this, $rule), $ruleParams); } elseif (method_exists($Validation, $rule)) { $valid = call_user_func_array(array(&$Validation, $rule), $ruleParams); } elseif (!is_array($validator['rule'])) { $valid = preg_match($rule, $data[$fieldName]); } if (!$valid) { if (!isset($validator['message'])) { if (is_string($index)) { $validator['message'] = $index; } else { $validator['message'] = ife(is_numeric($index) && count($ruleSet) > 1, $index + 1, $message); } } $this->invalidate($fieldName, $validator['message']); if ($validator['last']) { break; } } } } } } return $this->validationErrors; }
/** * Mapped method to find invalid metadata for an attached model. * * @param object $Model * @param array $data * @return mixed */ public function invalidMeta(&$Model, $data = array()) { extract($this->__settings[$Model->alias]); $this->_validationErrors[$Model->name] = $errors = array(); if (isset($validate) && !empty($validate)) { App::import('Core', 'Validation'); $Validation = Validation::getInstance(); $methods = array_map('strtolower', get_class_methods($Model)); foreach (Set::flatten($data, '/') as $k => $v) { $rules = Set::extract("/{$k}/.", $validate); if (!empty($rules)) { foreach ($rules as $ruleSet) { if (!Set::numeric(array_keys($ruleSet))) { $ruleSet = array($ruleSet); } foreach ($ruleSet as $rule) { if (isset($rule['allowEmpty']) && $rule['allowEmpty'] === true && $v == '') { break 2; } if (is_array($rule['rule'])) { $ruleName = array_shift($rule['rule']); $ruleParams = $rule['rule']; array_unshift($ruleParams, $v); } else { $ruleName = $rule['rule']; $ruleParams = array($v); } $valid = true; if (in_array(strtolower($ruleName), $methods)) { $valid = $Model->dispatchMethod($ruleName, $ruleParams); } elseif (method_exists($Validation, $ruleName)) { $valid = $Validation->dispatchMethod($ruleName, $ruleParams); } if (!$valid) { $ruleMessage = isset($rule['message']) && !empty($rule['message']) ? $rule['message'] : sprintf('%s %s', 'Not', $rule); $errors[] = $this->_unflatten($k, $ruleMessage, '/'); if (isset($rule['last']) && $rule['last'] === true) { break 3; } } } } } } } if (empty($errors)) { return false; } $tmp_errors = array(); foreach ($errors as $idx => $error) { foreach ($error as $field => $message) { $tmp_errors[$field] = $message; } } $this->_validationErrors[$Model->name] = $tmp_errors; $Model->validationErrors = array_merge($Model->validationErrors, array('Metadatum' => $tmp_errors)); return $errors; }