mirror of
https://github.com/e107inc/e107.git
synced 2025-08-05 22:27:34 +02:00
Validator 'enum' type added
This commit is contained in:
@@ -58,102 +58,102 @@ class e_validator
|
|||||||
* @var integer Unknown error code
|
* @var integer Unknown error code
|
||||||
*/
|
*/
|
||||||
const ERR_UNKNOWN = 0;
|
const ERR_UNKNOWN = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Value not found error code
|
* @var integer Value not found error code
|
||||||
*/
|
*/
|
||||||
const ERR_MISSING_VALUE = 101;
|
const ERR_MISSING_VALUE = 101;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Unexpected value type error code (bad rule)
|
* @var integer Unexpected value type error code (bad rule)
|
||||||
*/
|
*/
|
||||||
const ERR_UNEXPECTED_VALUE = 102;
|
const ERR_UNEXPECTED_VALUE = 102;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Invalid characters error code
|
* @var integer Invalid characters error code
|
||||||
*/
|
*/
|
||||||
const ERR_INVALID_CHARS = 103;
|
const ERR_INVALID_CHARS = 103;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Invalid email error code
|
* @var integer Invalid email error code
|
||||||
*/
|
*/
|
||||||
const ERR_INVALID_EMAIL = 104;
|
const ERR_INVALID_EMAIL = 104;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Invalid email error code
|
* @var integer Field doesn't match error code
|
||||||
*/
|
*/
|
||||||
const ERR_FIELDS_MATCH = 105;
|
const ERR_FIELDS_MATCH = 105;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer String too short error code
|
* @var integer String too short error code
|
||||||
*/
|
*/
|
||||||
const ERR_TOO_SHORT = 131;
|
const ERR_TOO_SHORT = 131;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer String too long error code
|
* @var integer String too long error code
|
||||||
*/
|
*/
|
||||||
const ERR_TOO_LONG = 132;
|
const ERR_TOO_LONG = 132;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Number too low error code
|
* @var integer Number too low error code
|
||||||
*/
|
*/
|
||||||
const ERR_TOO_LOW = 133;
|
const ERR_TOO_LOW = 133;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Number too high error code
|
* @var integer Number too high error code
|
||||||
*/
|
*/
|
||||||
const ERR_TOO_HIGH = 134;
|
const ERR_TOO_HIGH = 134;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Array count too low error code
|
* @var integer Array count too low error code
|
||||||
*/
|
*/
|
||||||
const ERR_ARRCOUNT_LOW = 135;
|
const ERR_ARRCOUNT_LOW = 135;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Array count high error code
|
* @var integer Array count high error code
|
||||||
*/
|
*/
|
||||||
const ERR_ARRCOUNT_HIGH = 136;
|
const ERR_ARRCOUNT_HIGH = 136;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Type of integer expected error code
|
* @var integer Type of integer expected error code
|
||||||
*/
|
*/
|
||||||
const ERR_INT_EXPECTED = 151;
|
const ERR_INT_EXPECTED = 151;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Type of float expected error code
|
* @var integer Type of float expected error code
|
||||||
*/
|
*/
|
||||||
const ERR_FLOAT_EXPECTED = 152;
|
const ERR_FLOAT_EXPECTED = 152;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Instance type expected error code
|
* @var integer Instance type expected error code
|
||||||
*/
|
*/
|
||||||
const ERR_INSTANCEOF_EXPECTED = 153;
|
const ERR_INSTANCEOF_EXPECTED = 153;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Array type expected error code
|
* @var integer Array type expected error code
|
||||||
*/
|
*/
|
||||||
const ERR_ARRAY_EXPECTED = 154;
|
const ERR_ARRAY_EXPECTED = 154;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Generic (empty value) error code
|
* @var integer Generic (empty value) error code
|
||||||
*/
|
*/
|
||||||
const ERR_GENERIC = 191;
|
const ERR_GENERIC = 191;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer File not exists or not a file error code
|
* @var integer File not exists or not a file error code
|
||||||
*/
|
*/
|
||||||
const ERR_NOT_FILE = 201;
|
const ERR_NOT_FILE = 201;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer File not writable error code
|
* @var integer File not writable error code
|
||||||
*/
|
*/
|
||||||
const ERR_WRITABLE_FILE = 202;
|
const ERR_WRITABLE_FILE = 202;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer File exceeds allowed file size error code
|
* @var integer File exceeds allowed file size error code
|
||||||
*/
|
*/
|
||||||
const ERR_SIZEMIN_FILE = 203;
|
const ERR_SIZEMIN_FILE = 203;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer File lower than minimal file size error code
|
* @var integer File lower than minimal file size error code
|
||||||
*/
|
*/
|
||||||
@@ -161,13 +161,13 @@ class e_validator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Required rules - Used by validate method
|
* Required rules - Used by validate method
|
||||||
*
|
*
|
||||||
* Structure: array(type, condition, field title LAN[, condition help, validation error message]);
|
* Structure: array(type, condition, field title LAN[, condition help, validation error message]);
|
||||||
*
|
*
|
||||||
* @example $_required_rules['download_category_id'] = array('int', '1', 'Download Category', 'choose category')
|
* @example $_required_rules['download_category_id'] = array('int', '1', 'Download Category', 'choose category')
|
||||||
*
|
*
|
||||||
* Validation array structure:
|
* Validation array structure:
|
||||||
* - type | condition =
|
* - type | condition =
|
||||||
* - regex | regex string
|
* - regex | regex string
|
||||||
* - email | no condition required
|
* - email | no condition required
|
||||||
* - int/integer | number range in format 'min:max'
|
* - int/integer | number range in format 'min:max'
|
||||||
@@ -176,55 +176,56 @@ class e_validator
|
|||||||
* - required | no condition required
|
* - required | no condition required
|
||||||
* - callback | string function name or array(class name|object, method) (static call)
|
* - callback | string function name or array(class name|object, method) (static call)
|
||||||
* - instanceof | string class name
|
* - instanceof | string class name
|
||||||
|
* - enum | (string) values separated by '#' e.g. 'match1#match2#match3'
|
||||||
* - array | array count range in format 'min:max'
|
* - array | array count range in format 'min:max'
|
||||||
* - compare | string field_name, value should be in format field_name => array('value1', 'value1')
|
* - compare | string field_name, value should be in format field_name => array('value1', 'value1')
|
||||||
* if value1 === value1, field_name => value1 will be added to $_valid_data array
|
* if value1 === value1, field_name => value1 will be added to $_valid_data array
|
||||||
* - field title LAN =
|
* - field title LAN =
|
||||||
* human readable field (data key) name
|
* human readable field (data key) name
|
||||||
* - [optional] condition help =
|
* - [optional] condition help =
|
||||||
* can be used for both inline field help and validation error message
|
* can be used for both inline field help and validation error message
|
||||||
* - [optional] validation error message =
|
* - [optional] validation error message =
|
||||||
* if empty condition help will be used
|
* if empty condition help will be used
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $_required_rules = array();
|
protected $_required_rules = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check data only if exist/non-empty
|
* Check data only if exist/non-empty
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $_optional_rules = array();
|
protected $_optional_rules = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains validation error codes in format 'field=>error_code'
|
* Contains validation error codes in format 'field=>error_code'
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $_validation_results = array();
|
protected $_validation_results = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores validated data (only after successful {@link validateField()} call
|
* Stores validated data (only after successful {@link validateField()} call
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $_valid_data = array();
|
protected $_valid_data = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores validate check result
|
* Stores validate check result
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
protected $_is_valid_data = true;
|
protected $_is_valid_data = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* eMessage handler namespace
|
* eMessage handler namespace
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $_message_stack = 'validator';
|
protected $_message_stack = 'validator';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructore
|
* Constructore
|
||||||
* @param string [optional] $message_stack [optional] eMessage handler namespace
|
* @param string [optional] $message_stack [optional] eMessage handler namespace
|
||||||
* @param array [optional] $rules validation rules
|
* @param array [optional] $rules validation rules
|
||||||
* @param array [optional] $optrules optional validation rules
|
* @param array [optional] $optrules optional validation rules
|
||||||
*/
|
*/
|
||||||
@@ -234,10 +235,10 @@ class e_validator
|
|||||||
->setRules($rules)
|
->setRules($rules)
|
||||||
->setOptionalRules($optrules);
|
->setOptionalRules($optrules);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set message stack
|
* Set message stack
|
||||||
*
|
*
|
||||||
* @param string $mstack
|
* @param string $mstack
|
||||||
* @return e_validator
|
* @return e_validator
|
||||||
*/
|
*/
|
||||||
@@ -247,7 +248,7 @@ class e_validator
|
|||||||
$this->_message_stack = $mstack;
|
$this->_message_stack = $mstack;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $rules
|
* @param array $rules
|
||||||
* @return e_validator
|
* @return e_validator
|
||||||
@@ -257,7 +258,7 @@ class e_validator
|
|||||||
$this->_required_rules = $rules;
|
$this->_required_rules = $rules;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $rules
|
* @param array $rules
|
||||||
* @return e_validator
|
* @return e_validator
|
||||||
@@ -267,9 +268,9 @@ class e_validator
|
|||||||
$this->_optional_rules = $rules;
|
$this->_optional_rules = $rules;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add successfully validated data to the valid array
|
* Add successfully validated data to the valid array
|
||||||
*
|
*
|
||||||
* @param string $field_name
|
* @param string $field_name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
@@ -280,7 +281,7 @@ class e_validator
|
|||||||
$this->_valid_data[$field_name] = $value;
|
$this->_valid_data[$field_name] = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -288,18 +289,18 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return $this->_valid_data;
|
return $this->_valid_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate data
|
* Validate data
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function validate($data)
|
function validate($data)
|
||||||
{
|
{
|
||||||
$this->reset();
|
$this->reset();
|
||||||
|
|
||||||
$rules = array_merge(array_keys($this->_required_rules), array_keys($this->_optional_rules));
|
$rules = array_merge(array_keys($this->_required_rules), array_keys($this->_optional_rules));
|
||||||
// no rules, no check
|
// no rules, no check
|
||||||
if(!$rules)
|
if(!$rules)
|
||||||
{
|
{
|
||||||
@@ -307,14 +308,14 @@ class e_validator
|
|||||||
$this->_valid_data = $data;
|
$this->_valid_data = $data;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($rules as $field_name)
|
foreach ($rules as $field_name)
|
||||||
{
|
{
|
||||||
$value = varset($data[$field_name], null);
|
$value = varset($data[$field_name], null);
|
||||||
$required = $this->isRequiredField($field_name);
|
$required = $this->isRequiredField($field_name);
|
||||||
if(($required || $this->isOptionalField($field_name)) && !$this->validateField($field_name, $value, $required))
|
if(($required || $this->isOptionalField($field_name)) && !$this->validateField($field_name, $value, $required))
|
||||||
{
|
{
|
||||||
$this->_is_valid_data = false;
|
$this->_is_valid_data = false;
|
||||||
$this->addValidateMessage($this->getFieldName($field_name, $required), $this->getErrorCode($field_name), $this->getFieldMessage($field_name, $value, $required));
|
$this->addValidateMessage($this->getFieldName($field_name, $required), $this->getErrorCode($field_name), $this->getFieldMessage($field_name, $value, $required));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -322,10 +323,10 @@ class e_validator
|
|||||||
|
|
||||||
return $this->_is_valid_data;
|
return $this->_is_valid_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if field is required
|
* Check if field is required
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
@@ -333,10 +334,10 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return isset($this->_required_rules[$name]);
|
return isset($this->_required_rules[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if there is optional rule for this field
|
* Check if there is optional rule for this field
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
@@ -344,10 +345,10 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return isset($this->_optional_rules[$name]);
|
return isset($this->_optional_rules[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve help for the required field
|
* Retrieve help for the required field
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
@@ -361,13 +362,13 @@ class e_validator
|
|||||||
{
|
{
|
||||||
$msg = (isset($this->_optional_rules[$name][3]) ? $this->_optional_rules[$name][3] : $default);
|
$msg = (isset($this->_optional_rules[$name][3]) ? $this->_optional_rules[$name][3] : $default);
|
||||||
}
|
}
|
||||||
|
|
||||||
return defset($msg, $msg);
|
return defset($msg, $msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve validation error message for the required field
|
* Retrieve validation error message for the required field
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return string
|
* @return string
|
||||||
@@ -390,10 +391,10 @@ class e_validator
|
|||||||
}
|
}
|
||||||
else $msg = $this->_optional_rules[$name][4];
|
else $msg = $this->_optional_rules[$name][4];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($msg ? defset($msg, $msg) : '');
|
return ($msg ? defset($msg, $msg) : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return string
|
* @return string
|
||||||
@@ -408,10 +409,10 @@ class e_validator
|
|||||||
{
|
{
|
||||||
$msg = (isset($this->_optional_rules[$name][2]) ? $this->_optional_rules[$name][2] : $name);
|
$msg = (isset($this->_optional_rules[$name][2]) ? $this->_optional_rules[$name][2] : $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return defset($msg, $msg);
|
return defset($msg, $msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate single field
|
* Validate single field
|
||||||
*
|
*
|
||||||
@@ -427,9 +428,9 @@ class e_validator
|
|||||||
$type = $this->_required_rules[$name][0];
|
$type = $this->_required_rules[$name][0];
|
||||||
$cond = $this->_required_rules[$name][1];
|
$cond = $this->_required_rules[$name][1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(empty($value))
|
if(empty($value))
|
||||||
{
|
{
|
||||||
switch($this->_optional_rules[$name][0])
|
switch($this->_optional_rules[$name][0])
|
||||||
{
|
{
|
||||||
@@ -437,15 +438,15 @@ class e_validator
|
|||||||
case 'integer':
|
case 'integer':
|
||||||
$value = 0;
|
$value = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'float':
|
case 'float':
|
||||||
$value = floatval($value);
|
$value = floatval($value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'array':
|
case 'array':
|
||||||
$value = array();
|
$value = array();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$value = '';
|
$value = '';
|
||||||
break;
|
break;
|
||||||
@@ -457,9 +458,9 @@ class e_validator
|
|||||||
$cond = $this->_optional_rules[$name][1];
|
$cond = $this->_optional_rules[$name][1];
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($type)
|
switch ($type)
|
||||||
{
|
{
|
||||||
case 'required':
|
case 'required':
|
||||||
if(empty($value))
|
if(empty($value))
|
||||||
{
|
{
|
||||||
$this->addValidateResult($name, self::ERR_GENERIC);
|
$this->addValidateResult($name, self::ERR_GENERIC);
|
||||||
@@ -468,7 +469,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'email':
|
case 'email':
|
||||||
if(!check_email($value))
|
if(!check_email($value))
|
||||||
{
|
{
|
||||||
@@ -478,7 +479,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'regex':
|
case 'regex':
|
||||||
if(!preg_match($cond, $value))
|
if(!preg_match($cond, $value))
|
||||||
{
|
{
|
||||||
@@ -488,7 +489,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'callback':
|
case 'callback':
|
||||||
if(!call_user_func($cond, $value))
|
if(!call_user_func($cond, $value))
|
||||||
{
|
{
|
||||||
@@ -498,7 +499,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'instanceof':
|
case 'instanceof':
|
||||||
if(!(is_object($value) && $value instanceof $cond))
|
if(!(is_object($value) && $value instanceof $cond))
|
||||||
{
|
{
|
||||||
@@ -508,7 +509,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'int':
|
case 'int':
|
||||||
case 'integer':
|
case 'integer':
|
||||||
if(!preg_match('/^-?[\d]+$/', $value)) // negative values support
|
if(!preg_match('/^-?[\d]+$/', $value)) // negative values support
|
||||||
@@ -531,7 +532,7 @@ class e_validator
|
|||||||
$this->addValidData($name, intval($value));
|
$this->addValidData($name, intval($value));
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'str':
|
case 'str':
|
||||||
case 'string':
|
case 'string':
|
||||||
case 'text':
|
case 'text':
|
||||||
@@ -543,9 +544,9 @@ class e_validator
|
|||||||
$this->addValidateResult($name, self::ERR_TOO_SHORT);
|
$this->addValidateResult($name, self::ERR_TOO_SHORT);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if('varchar' == $type && !varset($tmp[1])) $tmp[1] = 255;
|
if('varchar' == $type && !varset($tmp[1])) $tmp[1] = 255;
|
||||||
|
|
||||||
if(is_numeric(varset($tmp[1])) && (integer) $tmp[1] < $length)
|
if(is_numeric(varset($tmp[1])) && (integer) $tmp[1] < $length)
|
||||||
{
|
{
|
||||||
$this->addValidateResult($name, self::ERR_TOO_LONG);
|
$this->addValidateResult($name, self::ERR_TOO_LONG);
|
||||||
@@ -555,6 +556,18 @@ class e_validator
|
|||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'enum':
|
||||||
|
$tmp = explode('#', $cond);
|
||||||
|
if(!$value || !in_array($value, $tmp))
|
||||||
|
{
|
||||||
|
$this->addValidateResult($name, self::ERR_FIELDS_MATCH);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->addValidData($name, (string) $value);
|
||||||
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'float':
|
case 'float':
|
||||||
if(!is_numeric($value))
|
if(!is_numeric($value))
|
||||||
{
|
{
|
||||||
@@ -575,7 +588,7 @@ class e_validator
|
|||||||
$this->addValidData($name, (float) $value);
|
$this->addValidData($name, (float) $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'array':
|
case 'array':
|
||||||
if(!is_array($value))
|
if(!is_array($value))
|
||||||
{
|
{
|
||||||
@@ -597,7 +610,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'file': // TODO - type image - validate dimensions?
|
case 'file': // TODO - type image - validate dimensions?
|
||||||
parse_str($cond, $params);
|
parse_str($cond, $params);
|
||||||
$path = e107::getParser()->replaceConstants(varset($params['base']).$value);
|
$path = e107::getParser()->replaceConstants(varset($params['base']).$value);
|
||||||
@@ -615,7 +628,7 @@ class e_validator
|
|||||||
{
|
{
|
||||||
$tmp = $this->parseMinMax($cond);
|
$tmp = $this->parseMinMax($cond);
|
||||||
$fs = filesize($path);
|
$fs = filesize($path);
|
||||||
if(!$fs || (integer) $tmp[0] > $fs)
|
if(!$fs || (integer) $tmp[0] > $fs)
|
||||||
{
|
{
|
||||||
$this->addValidateResult($name, self::ERR_SIZEMIN_FILE);
|
$this->addValidateResult($name, self::ERR_SIZEMIN_FILE);
|
||||||
return false;
|
return false;
|
||||||
@@ -634,28 +647,28 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'compare':
|
case 'compare':
|
||||||
if(!is_array($value))
|
if(!is_array($value))
|
||||||
{
|
{
|
||||||
$this->addValidateResult($name, self::ERR_UNEXPECTED_VALUE);
|
$this->addValidateResult($name, self::ERR_UNEXPECTED_VALUE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if('varchar' == $type && !varset($tmp[1])) $tmp[1] = 255;
|
if('varchar' == $type && !varset($tmp[1])) $tmp[1] = 255;
|
||||||
|
|
||||||
if(is_numeric(varset($tmp[1])) && (integer) $tmp[1] < $length)
|
if(is_numeric(varset($tmp[1])) && (integer) $tmp[1] < $length)
|
||||||
{
|
{
|
||||||
$this->addValidateResult($name, self::ERR_TOO_LONG);
|
$this->addValidateResult($name, self::ERR_TOO_LONG);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!($value[0] && $value[1] && $value[0] == $value[1]))
|
if(!($value[0] && $value[1] && $value[0] == $value[1]))
|
||||||
{
|
{
|
||||||
$this->addValidateResult($name, self::ERR_FIELDS_MATCH);
|
$this->addValidateResult($name, self::ERR_FIELDS_MATCH);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check length
|
// check length
|
||||||
if($cond)
|
if($cond)
|
||||||
{
|
{
|
||||||
@@ -675,7 +688,7 @@ class e_validator
|
|||||||
$this->addValidData($name, $value[0]);
|
$this->addValidData($name, $value[0]);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'compare_strict':
|
case 'compare_strict':
|
||||||
if(!is_array($value))
|
if(!is_array($value))
|
||||||
{
|
{
|
||||||
@@ -687,7 +700,7 @@ class e_validator
|
|||||||
$this->addValidateResult($name, self::ERR_FIELDS_MATCH);
|
$this->addValidateResult($name, self::ERR_FIELDS_MATCH);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check length
|
// check length
|
||||||
if($cond)
|
if($cond)
|
||||||
{
|
{
|
||||||
@@ -707,29 +720,29 @@ class e_validator
|
|||||||
$this->addValidData($name, $value[0]);
|
$this->addValidData($name, $value[0]);
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$this->addValidateResult($name, self::ERR_UNEXPECTED_VALUE);
|
$this->addValidateResult($name, self::ERR_UNEXPECTED_VALUE);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseMinMax($string)
|
protected function parseMinMax($string)
|
||||||
{
|
{
|
||||||
return explode(':', $this->_convertConditionBC($string), 2);
|
return explode(':', $this->_convertConditionBC($string), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _convertConditionBC($condition)
|
private function _convertConditionBC($condition)
|
||||||
{
|
{
|
||||||
// BC! Will be removed after we replace '-' with ':' separator!
|
// BC! Will be removed after we replace '-' with ':' separator!
|
||||||
if(strpos($condition, ':') === false)
|
if(strpos($condition, ':') === false)
|
||||||
{
|
{
|
||||||
return preg_replace('/^([0-9]+)-([0-9]+)$/', '$1:$2', $condition);
|
return preg_replace('/^([0-9]+)-([0-9]+)$/', '$1:$2', $condition);
|
||||||
}
|
}
|
||||||
return $condition;
|
return $condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add validation error to validate result stack
|
* Add validation error to validate result stack
|
||||||
*
|
*
|
||||||
@@ -746,25 +759,25 @@ class e_validator
|
|||||||
e107::getMessage()->addStack(sprintf($err_message, $err_code, $field_title), $this->_message_stack, (true === $custom ? E_MESSAGE_ERROR : $custom));
|
e107::getMessage()->addStack(sprintf($err_message, $err_code, $field_title), $this->_message_stack, (true === $custom ? E_MESSAGE_ERROR : $custom));
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Core message
|
//Core message
|
||||||
$msg = sprintf(
|
$msg = sprintf(
|
||||||
LAN_VALIDATE_FAILMSG, //'<strong>"%s"</strong> validation error: [#%d] %s. '
|
LAN_VALIDATE_FAILMSG, //'<strong>"%s"</strong> validation error: [#%d] %s. '
|
||||||
$field_title,
|
$field_title,
|
||||||
$err_code,
|
$err_code,
|
||||||
$this->getErrorByCode($err_code)
|
$this->getErrorByCode($err_code)
|
||||||
);
|
);
|
||||||
|
|
||||||
//Additional message
|
//Additional message
|
||||||
if($err_message)
|
if($err_message)
|
||||||
{
|
{
|
||||||
$msg .= ' '.$err_message;
|
$msg .= ' '.$err_message;
|
||||||
}
|
}
|
||||||
e107::getMessage()->addStack($msg, $this->_message_stack, E_MESSAGE_ERROR);
|
e107::getMessage()->addStack($msg, $this->_message_stack, E_MESSAGE_ERROR);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get validate message array
|
* Get validate message array
|
||||||
*
|
*
|
||||||
@@ -775,10 +788,10 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return e107::getMessage()->getAll($this->_message_stack, true, $clear);
|
return e107::getMessage()->getAll($this->_message_stack, true, $clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render validate messages
|
* Render validate messages
|
||||||
*
|
*
|
||||||
* @param boolean $session merge with session messages
|
* @param boolean $session merge with session messages
|
||||||
* @param boolean $clear
|
* @param boolean $clear
|
||||||
* @return string
|
* @return string
|
||||||
@@ -787,7 +800,7 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return e107::getMessage()->render($this->_message_stack, $session, $clear);
|
return e107::getMessage()->render($this->_message_stack, $session, $clear);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param boolean $session clear session messages as well, default true
|
* @param boolean $session clear session messages as well, default true
|
||||||
* @return e_validator
|
* @return e_validator
|
||||||
@@ -797,7 +810,7 @@ class e_validator
|
|||||||
e107::getMessage()->reset(false, $this->_message_stack, $session);
|
e107::getMessage()->reset(false, $this->_message_stack, $session);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add validate error code for a field
|
* Add validate error code for a field
|
||||||
*
|
*
|
||||||
@@ -810,7 +823,7 @@ class e_validator
|
|||||||
$this->_validation_results[$name] = $code;
|
$this->_validation_results[$name] = $code;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get validate result array
|
* Get validate result array
|
||||||
*
|
*
|
||||||
@@ -821,7 +834,7 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return $this->_validation_results;
|
return $this->_validation_results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get validate result for a field
|
* Get validate result for a field
|
||||||
*
|
*
|
||||||
@@ -833,7 +846,7 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return (isset($this->_validation_results[$field]) ? $this->_validation_results[$field] : $default);
|
return (isset($this->_validation_results[$field]) ? $this->_validation_results[$field] : $default);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get error string by given error code
|
* Get error string by given error code
|
||||||
*
|
*
|
||||||
@@ -845,7 +858,7 @@ class e_validator
|
|||||||
$lan = 'LAN_VALIDATE_'.$error_code;
|
$lan = 'LAN_VALIDATE_'.$error_code;
|
||||||
return defset($lan, $lan);
|
return defset($lan, $lan);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return e_validator
|
* @return e_validator
|
||||||
*/
|
*/
|
||||||
@@ -854,7 +867,7 @@ class e_validator
|
|||||||
$this->_validation_results = array();
|
$this->_validation_results = array();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
@@ -862,7 +875,7 @@ class e_validator
|
|||||||
{
|
{
|
||||||
return empty($this->_is_valid_data);
|
return empty($this->_is_valid_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset object validate result data
|
* Reset object validate result data
|
||||||
* @return e_validator
|
* @return e_validator
|
||||||
@@ -873,7 +886,7 @@ class e_validator
|
|||||||
$this->_valid_data = array();
|
$this->_valid_data = array();
|
||||||
$this->clearValidateResults()
|
$this->clearValidateResults()
|
||||||
->clearValidateMessages();
|
->clearValidateMessages();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -918,7 +931,7 @@ class validatorClass
|
|||||||
foreach ($definitions as $dest => $defs)
|
foreach ($definitions as $dest => $defs)
|
||||||
{
|
{
|
||||||
$errNum = 0; // Start with no error
|
$errNum = 0; // Start with no error
|
||||||
|
|
||||||
if(!is_array($defs)) //default rule - dbClean -> toDB
|
if(!is_array($defs)) //default rule - dbClean -> toDB
|
||||||
{
|
{
|
||||||
$defs = array('dbClean', ($defs ? $defs : 'toDB'));
|
$defs = array('dbClean', ($defs ? $defs : 'toDB'));
|
||||||
@@ -972,7 +985,7 @@ class validatorClass
|
|||||||
}
|
}
|
||||||
if (!$errNum && isset($defs['minLength']) && ($tp->ustrlen($value) < $defs['minLength']))
|
if (!$errNum && isset($defs['minLength']) && ($tp->ustrlen($value) < $defs['minLength']))
|
||||||
{
|
{
|
||||||
if ($value == '')
|
if ($value == '')
|
||||||
{
|
{
|
||||||
if (!varsettrue($defs['fieldOptional']))
|
if (!varsettrue($defs['fieldOptional']))
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user