07.18
php
saved
wargoth
Note
Advanced validation by By Evan Sagge aka "evan", validation.php (should be placed in app/vendors/validation dir).
Advanced validation by By Evan Sagge aka "evan", validation.php (should be placed in app/vendors/validation dir).
- <?php
- /**
- * Validation class.
- *
- * This file contaings the validation class for CakePHP.
- *
- * PHP versions 4 and 5
- *
- * @author Evan Sagge <evansagge@gmail.com>
- * @package evansagge
- * @subpackage evansagge.cake.model.util
- */
- /**
- * Validation class.
- *
- * This class contains methods for implementing advanced model validation in CakePHP.
- * Validation for a field is enabled through the {@link @AppModel::validate} variable.
- *
- * PHP versions 4 and 5
- *
- * @author Evan Sagge <evansagge@gmail.com>
- * @package evansagge
- * @subpackage evansagge.cake
- * @see AppModel
- */
- class Validation
- {
- /**
- * Reference for model data
- */
- var $data;
- /**
- * Reference for model object
- */
- var $model;
- var $errorCount = 0;
- /**
- * Constructor for validation class. This initializes the data to validate as well as the model
- * against which data should be validated.
- *
- * @param mixed $data The data to validated.
- * @param object $model The model object against which the data should be validated.
- * @return Validation
- */
- function Validation(&$data, &$model)
- {
- $this->data =& $data;
- $this->model =& $model;
- $this->name =& $this->model->name;
- }
- /**
- * Evaluates the given validation result. If the value is set to true, it will return true;
- * otherwise, it has two options: if $params['message'] is defined, it will add its value to
- * the model object's $validationErrors array and return false, else it will add the value of
- * the concatenation of the humanized field name and the passed $messageOnFail string to the
- * model object's $validationErrors array and return false.
- *
- * @param bool $validation The validation result.
- * @param string $messageOnFail The default message to return if the validation results to
- * false.
- * @param string $fieldName The field name.
- * @param array $params Extra validation parameters.
- * @return Validation
- */
- {
- if ($validation)
- {
- return true;
- }
- $this->model->invalidate($fieldName);
- $this->errorCount++;
- return false;
- }
- /**
- * Checks if the value defined by the field name is not empty.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is not empty; false otherwise.
- */
- function validateNotEmpty($fieldName, $params)
- {
- }
- /**
- * Alias for Validation::validateNotEmpty()
- */
- function validateRequired($fieldName, $params)
- {
- return $this->_evaluate($this->validateNotEmpty($fieldName, $params), $fieldName, $params);
- }
- /**
- * Matches the value defined by the field name against the pattern specified by
- * $params['pattern'].
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Contains the pattern to match the value of the field name against.
- * @return bool True if pattern matches the value of the field name; false otherwise.
- */
- function validatePattern($fieldName, $params)
- {
- $pattern = $params['pattern'];
- return $this->_evaluate(preg_match($pattern, $this->data[$this->name][$fieldName]), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is a valid word, i.e. contains only
- * alphanumeric characters or the underscore ('_') character.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True value of the field name is a valid word; false otherwise.
- * @see Validation::validatePattern()
- */
- function validateWord($fieldName, $params)
- {
- $params['pattern'] = '/^\\w*$/';
- return $this->_evaluate($this->validatePattern($fieldName, $params), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is an integer.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True value of the field name is an integer; false otherwise.
- * @see Validation::validatePattern()
- */
- function validateInteger($fieldName, $params)
- {
- $params['pattern'] = '/^\\d+$/';
- return $this->_evaluate($this->validatePattern($fieldName, $params), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is a number.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True value of the field name is a floating point number; false otherwise.
- * @see Validation::validatePattern()
- */
- function validateNumber($fieldName, $params)
- {
- {
- $params['pattern'] = '/^\\d+$/';
- }
- else
- {
- $params['pattern'] = '/^(\\d+)|(\\d*\.\\d+)$/';
- }
- return $this->_evaluate($this->validatePattern($fieldName, $params), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name has a valid e-mail address format.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name has a valid e-mail address format; false
- * otherwise.
- * @see Validation::validatePattern()
- */
- function validateEmail($fieldName, $params)
- {
- $params['pattern'] = '/\\A(?:^([a-z0-9][a-z0-9_\\-\\.\\+]*)@([a-z0-9]'
- . '[a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|'
- . '[a-z]{2,4}))$)\\z/i';
- return $this->_evaluate($this->validatePattern($fieldName, $params), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is a valid value for a year.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is a valid value for a year; false
- * otherwise.
- * @see Validation::validatePattern()
- */
- function validateYear($fieldName)
- {
- $params['pattern'] = '/^[12][0-9]{3}$/';
- return $this->_evaluate($this->validatePattern($fieldName, $params), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is unique for the given data model. The
- * check for uniqueness is case-insensitive. If $params['conditions'] is given,
- * this is used as a constraint. If $params['scope'] is given, the value of
- * the field name is only checked against records that match the value of the
- * column/field defined by $params['scope'].
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is unique; false otherwise.
- * @see Model::hasAny()
- */
- function validateUnique($fieldName, $params)
- {
- $val = $this->data[$this->name][$fieldName];
- $column = $this->name . '.' . $fieldName;
- $id = $this->name . '.' . $this->model->primaryKey;
- {
- $conditions = $params['conditions'];
- }
- {
- {
- foreach ($params['scope'] as $scope)
- {
- $conditions[$scope] = $this->data[$this->name][$scope];
- }
- }
- {
- $conditions[$params['scope']] = $this->data[$this->name][$params['scope']];
- }
- }
- $conditions[$column] = $val;
- {
- $conditions[$id] = ('!=' . $this->data[$this->name][$this->model->primaryKey]);
- }
- return $this->_evaluate(!$this->model->hasAny($conditions), $fieldName, $params);
- }
- /**
- * Checks if the length of the string value defined by the field name is within the range
- * specified by $params['min'], $params['max'], or both.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if length of the value of the field name is within the specified range;
- * false otherwise.
- */
- function validateLength($fieldName, $params)
- {
- $val = $this->data[$this->name][$fieldName];
- {
- return $this->_evaluate($length >= $params['min'] && $length <= $params['max'], $fieldName, $params);
- }
- {
- return $this->_evaluate($length >= $params['min'], $fieldName, $params);
- }
- {
- return $this->_evaluate($length <= $params['max'], $fieldName, $params);
- }
- }
- /**
- * Checks if the numeric value defined by the field name is within the range
- * specified by $params['min'], $params['max'], or both.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if numeric value of the field name is within the specified range;
- * false otherwise.
- */
- function validateRange($fieldName, $params)
- {
- if ($result = $this->validateNumber($fieldName, $params))
- {
- return $result;
- }
- $val = $this->data[$this->name][$fieldName];
- {
- return $this->_evaluate($val >= $params['min'] && $val <= $params['max'], $fieldName, $params);
- }
- {
- return $this->_evaluate($val >= $params['min'], $fieldName, $params);
- }
- {
- return $this->_evaluate($val <= $params['max'], $fieldName, $params);
- }
- }
- /**
- * Checks if the value defined by the field name corresponds with it's confirmation value,
- * which is defined by the field specified in {@link $params}['confirm_var'] if defined,
- * or by <the field name>_confirmation.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name corresponds to its confirmation
- * value; false otherwise.
- */
- function validateConfirmed($fieldName, $params)
- {
- $val = $this->data[$this->name][$fieldName];
- {
- $confirmVar = $params['confirm_var'];
- }
- else
- {
- {
- $returnValue = false;
- }
- $confirmVar = $fieldName . '_confirmation';
- }
- {
- $this->data[$this->name][$confirmVar] = null;
- }
- return $this->_evaluate($val == $this->data[$this->name][$confirmVar], $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is a file.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is a file; false otherwise.
- */
- function validateFile($fieldName, $params)
- {
- $file = $this->data[$this->name][$fieldName];
- $returnValue = true;
- if ($file['error'] == UPLOAD_ERR_OK)
- {
- {
- $returnValue = false;
- }
- }
- else
- {
- }
- return $this->_evaluate($returnValue, $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is an image file.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is an image file; false otherwise.
- * @see Validation::validateFile()
- */
- function validateImageFile($fieldName, $params)
- {
- 'image/x-png', 'image/x-jg', 'image/gif');
- return $this->_evaluate($this->validateFile($fieldName, $params), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is a properly uploaded file.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is a properly uploaded file; false otherwise.
- */
- function validateUploaded($fieldName, $params)
- {
- return $this->_evaluate(is_uploaded_file($this->data[$this->name][$fieldName]['tmp_name']), $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is a date set in the future. This
- * automatically checks if the value is in proper date format.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is a future date; false otherwise.
- * @see Validation::validateDate()
- */
- function validateFutureDate($fieldName, $params)
- {
- if ($result = $this->validateDate($fieldName, $params))
- {
- return $result;
- }
- }
- /**
- * Checks if the value defined by the field name is in proper date format (yyyy-mm-dd).
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is in proper date format; false otherwise.
- */
- function validateDate($fieldName, $params)
- {
- $date = $this->data[$this->name][$fieldName];
- $datePattern = '/^\d{4}-\d?\d-\d?\d$/';
- {
- }
- else
- {
- $result = false;
- }
- return $this->_evaluate($result, $fieldName, $params);
- }
- /**
- * Checks if the value defined by the field name is in proper datetime format
- * (yyyy-mm-dd HH:MM:SS).
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is in proper datetime format; false otherwise.
- */
- function validateDatetime($fieldName, $params)
- {
- $dateTime = $this->data[$this->name][$fieldName];
- $dateTimePattern = '/^\d{4}-\d?\d-\d?\d '
- . '([01]?[0-9]|[2][0-4]):([0-5]?[0-9]):([0-5]?[0-9])$/';
- {
- }
- else
- {
- $result = false;
- }
- return $this->_evaluate($result, $fieldName, $params);
- }
- /**
- * Runs a method in the model object, passing to it the value of the specified field and the
- * additional parameters. The method's name is checked from the value of $params['method']; if
- * this is not available, then this function will try to call validate{Fieldname} instead. If
- * the method call fails, this function will return false.
- *
- * @param string $fieldName The name of the field to validate.
- * @param array $params Extra validation parameters.
- * @return bool True if value of the field name is in proper datetime format; false otherwise.
- */
- function validateMethod($fieldName, $params)
- {
- . Inflector::humanize($fieldName);
- {
- $this->errorCount++;
- return false;
- }
- else
- {
- $params))
- {
- return true;
- }
- else
- {
- $this->errorCount++;
- return false;
- }
- }
- }
- }
Parsed in 0.436 seconds, using GeSHi 1.0.7.14