mirror of
https://github.com/e107inc/e107.git
synced 2025-08-05 14:17:49 +02:00
Validator - language file and handler details
This commit is contained in:
@@ -9,12 +9,14 @@
|
|||||||
* Handler - general purpose validation functions
|
* Handler - general purpose validation functions
|
||||||
*
|
*
|
||||||
* $Source: /cvs_backup/e107_0.8/e107_handlers/validator_class.php,v $
|
* $Source: /cvs_backup/e107_0.8/e107_handlers/validator_class.php,v $
|
||||||
* $Revision: 1.18 $
|
* $Revision: 1.19 $
|
||||||
* $Date: 2010-01-12 13:11:48 $
|
* $Date: 2010-02-07 12:13:43 $
|
||||||
* $Author: secretr $
|
* $Author: secretr $
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
if (!defined('e107_INIT')) { exit; }
|
||||||
|
|
||||||
// List of error numbers which may be returned from validation
|
// List of error numbers which may be returned from validation
|
||||||
define('ERR_MISSING_VALUE','01');
|
define('ERR_MISSING_VALUE','01');
|
||||||
define('ERR_UNEXPECTED_VALUE','02');
|
define('ERR_UNEXPECTED_VALUE','02');
|
||||||
@@ -38,6 +40,9 @@ define('ERR_GENERIC', '19'); // This requires coder-defined error text
|
|||||||
define('ERR_IMAGE_TOO_WIDE', '20');
|
define('ERR_IMAGE_TOO_WIDE', '20');
|
||||||
define('ERR_IMAGE_TOO_HIGH', '21');
|
define('ERR_IMAGE_TOO_HIGH', '21');
|
||||||
|
|
||||||
|
// Default error messages
|
||||||
|
e107::includeLan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_validator.php');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validator class - used by e_model and its child classes
|
* Validator class - used by e_model and its child classes
|
||||||
*
|
*
|
||||||
@@ -52,7 +57,7 @@ class e_validator
|
|||||||
/**
|
/**
|
||||||
* @var integer Unknown error code
|
* @var integer Unknown error code
|
||||||
*/
|
*/
|
||||||
const ERR_UNKNOWN = -1;
|
const ERR_UNKNOWN = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Value not found error code
|
* @var integer Value not found error code
|
||||||
@@ -60,7 +65,7 @@ class e_validator
|
|||||||
const ERR_MISSING_VALUE = 101;
|
const ERR_MISSING_VALUE = 101;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Unexpected value error code (bad rule)
|
* @var integer Unexpected value type error code (bad rule)
|
||||||
*/
|
*/
|
||||||
const ERR_UNEXPECTED_VALUE = 102;
|
const ERR_UNEXPECTED_VALUE = 102;
|
||||||
|
|
||||||
@@ -125,7 +130,7 @@ class e_validator
|
|||||||
const ERR_INSTANCEOF_EXPECTED = 153;
|
const ERR_INSTANCEOF_EXPECTED = 153;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer Type of array expected error code
|
* @var integer Array type expected error code
|
||||||
*/
|
*/
|
||||||
const ERR_ARRAY_EXPECTED = 154;
|
const ERR_ARRAY_EXPECTED = 154;
|
||||||
|
|
||||||
@@ -134,6 +139,26 @@ class e_validator
|
|||||||
*/
|
*/
|
||||||
const ERR_GENERIC = 191;
|
const ERR_GENERIC = 191;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var integer File not exists or not a file error code
|
||||||
|
*/
|
||||||
|
const ERR_NOT_FILE = 201;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var integer File not writable error code
|
||||||
|
*/
|
||||||
|
const ERR_WRITABLE_FILE = 202;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var integer File exceeds allowed file size error code
|
||||||
|
*/
|
||||||
|
const ERR_SIZEMIN_FILE = 203;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var integer File lower than minimal file size error code
|
||||||
|
*/
|
||||||
|
const ERR_SIZEMAX_FILE = 204;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Required rules - Used by validate method
|
* Required rules - Used by validate method
|
||||||
*
|
*
|
||||||
@@ -272,7 +297,6 @@ class e_validator
|
|||||||
*/
|
*/
|
||||||
function validate($data)
|
function validate($data)
|
||||||
{
|
{
|
||||||
//XXX add direct e_model $data type support?
|
|
||||||
$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));
|
||||||
@@ -291,7 +315,7 @@ class e_validator
|
|||||||
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, $required));
|
$this->addValidateMessage($this->getFieldName($field_name, $required), $this->getErrorCode($field_name), $this->getFieldMessage($field_name, $required, $value));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -345,28 +369,29 @@ class e_validator
|
|||||||
* 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
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function getFieldMessage($name, $required = true)
|
function getFieldMessage($name, $value = '', $required = true)
|
||||||
{
|
{
|
||||||
if($required)
|
if($required)
|
||||||
{
|
{
|
||||||
if(!isset($this->_required_rules[$name][4]))
|
if(!isset($this->_required_rules[$name][4]))
|
||||||
{
|
{
|
||||||
return $this->getFieldHelp($name, true, 'Invalid value');
|
$msg = $this->getFieldHelp($name, true);
|
||||||
}
|
}
|
||||||
$msg = $this->_required_rules[$name][4];
|
else $msg = $this->_required_rules[$name][4];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(!isset($this->_optional_rules[$name][4]))
|
if(!isset($this->_optional_rules[$name][4]))
|
||||||
{
|
{
|
||||||
return $this->getFieldHelp($name, false, 'Invalid value');
|
$msg = $this->getFieldHelp($name, false);
|
||||||
}
|
}
|
||||||
$msg = $this->_optional_rules[$name][4];
|
else $msg = $this->_optional_rules[$name][4];
|
||||||
}
|
}
|
||||||
|
|
||||||
return defset($msg, $msg);
|
return ($msg ? defset($msg, $msg) : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -397,7 +422,14 @@ class e_validator
|
|||||||
*/
|
*/
|
||||||
function validateField($name, $value, $required = true)
|
function validateField($name, $value, $required = true)
|
||||||
{
|
{
|
||||||
if(!$required && empty($value))
|
if($required)
|
||||||
|
{
|
||||||
|
$type = $this->_required_rules[$name][0];
|
||||||
|
$cond = $this->_required_rules[$name][1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(empty($value))
|
||||||
{
|
{
|
||||||
switch($this->_optional_rules[$name][0])
|
switch($this->_optional_rules[$name][0])
|
||||||
{
|
{
|
||||||
@@ -421,16 +453,10 @@ class e_validator
|
|||||||
$this->addValidData($name, $value);
|
$this->addValidData($name, $value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if($required)
|
|
||||||
{
|
|
||||||
$type = $this->_required_rules[$name][0];
|
|
||||||
$cond = $this->_required_rules[$name][1];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$type = $this->_optional_rules[$name][0];
|
$type = $this->_optional_rules[$name][0];
|
||||||
$cond = $this->_optional_rules[$name][1];
|
$cond = $this->_optional_rules[$name][1];
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($type)
|
switch ($type)
|
||||||
{
|
{
|
||||||
case 'required':
|
case 'required':
|
||||||
@@ -566,6 +592,38 @@ class e_validator
|
|||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'file': // TODO - type image - validate dimensions?
|
||||||
|
parse_str($cond, $params);
|
||||||
|
$path = e107::getParser()->replaceConstants(varset($params['base']).$value);
|
||||||
|
if(is_file($path))
|
||||||
|
{
|
||||||
|
$this->addValidateResult($name, self::ERR_NOT_FILE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(vartrue($params['writable']) && !is_writable($path))
|
||||||
|
{
|
||||||
|
$this->addValidateResult($name, self::ERR_WRITABLE_FILE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(vartrue($params['size']))
|
||||||
|
{
|
||||||
|
$tmp = explode('-', $params['size'], 2);
|
||||||
|
$fs = filesize($path);
|
||||||
|
if(!$fs || $fs < (integer) $tmp[0])
|
||||||
|
{
|
||||||
|
$this->addValidateResult($name, self::ERR_SIZEMIN_FILE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
elseif(is_numeric(varset($tmp[1])) && $fs > (integer) $tmp[1])
|
||||||
|
{
|
||||||
|
$this->addValidateResult($name, self::ERR_SIZEMAX_FILE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->addValidData($name, $value);
|
||||||
|
return true;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'compare':
|
case 'compare':
|
||||||
if(!is_array($value))
|
if(!is_array($value))
|
||||||
{
|
{
|
||||||
@@ -622,7 +680,7 @@ class e_validator
|
|||||||
|
|
||||||
//Core message
|
//Core message
|
||||||
$msg = sprintf(
|
$msg = sprintf(
|
||||||
'<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)
|
||||||
@@ -631,7 +689,7 @@ class e_validator
|
|||||||
//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);
|
||||||
|
|
||||||
@@ -702,7 +760,7 @@ class e_validator
|
|||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
* @return integer error code
|
* @return integer error code
|
||||||
*/
|
*/
|
||||||
function getErrorCode($field, $default = -1)
|
function getErrorCode($field, $default = 0)
|
||||||
{
|
{
|
||||||
return (isset($this->_validation_results[$field]) ? $this->_validation_results[$field] : $default);
|
return (isset($this->_validation_results[$field]) ? $this->_validation_results[$field] : $default);
|
||||||
}
|
}
|
||||||
|
44
e107_languages/English/admin/lan_validator.php
Normal file
44
e107_languages/English/admin/lan_validator.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* Copyright 2008-2010 e107 Inc e107.org, Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
|
||||||
|
* $Id: lan_validator.php,v 1.1 2010-02-07 12:13:43 secretr Exp $
|
||||||
|
*
|
||||||
|
* Validator Handler - Language File
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Default error messages by Error code number
|
||||||
|
*/
|
||||||
|
define('LAN_VALIDATE_0', 'Unknown Error');
|
||||||
|
define('LAN_VALIDATE_101', 'Missing value');
|
||||||
|
define('LAN_VALIDATE_102', 'Unexpected value type');
|
||||||
|
define('LAN_VALIDATE_103', 'Invalid characters found');
|
||||||
|
define('LAN_VALIDATE_104', 'Not a valid email address');
|
||||||
|
define('LAN_VALIDATE_105', 'Fields don\'t match' );
|
||||||
|
define('LAN_VALIDATE_131', 'String too short');
|
||||||
|
define('LAN_VALIDATE_132', 'String too long');
|
||||||
|
define('LAN_VALIDATE_133', 'Number too low');
|
||||||
|
define('LAN_VALIDATE_134', 'Number too high');
|
||||||
|
define('LAN_VALIDATE_135', 'Array count too low');
|
||||||
|
define('LAN_VALIDATE_136', 'Array count too high');
|
||||||
|
define('LAN_VALIDATE_151', 'Number of type integer expected');
|
||||||
|
define('LAN_VALIDATE_152', 'Number of type float expected');
|
||||||
|
define('LAN_VALIDATE_153', 'Instance type expected');
|
||||||
|
define('LAN_VALIDATE_154', 'Array type expected');
|
||||||
|
define('LAN_VALIDATE_191', 'Empty value');
|
||||||
|
define('LAN_VALIDATE_201', 'File not exists');
|
||||||
|
define('LAN_VALIDATE_202', 'File not writable');
|
||||||
|
define('LAN_VALIDATE_203', 'File exceeds allowed file size');
|
||||||
|
define('LAN_VALIDATE_204', 'File size lower than allowed minimal file size');
|
||||||
|
//define('LAN_VALIDATE_', '');
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TRANSLATION INSTRUCTIONS:
|
||||||
|
* Don't translate %1$s, %2$s, %3$s, etc.
|
||||||
|
*
|
||||||
|
* These are substituted by validator handler:
|
||||||
|
* %1$s - field name
|
||||||
|
* %2$d - validation error code (number)
|
||||||
|
* %3$s - validation error message (string)
|
||||||
|
*/
|
||||||
|
define('LAN_VALIDATE_FAILMSG', '<strong>"%1$s"</strong> validation error: [#%2$d] %3$s.');
|
Reference in New Issue
Block a user