allow multiple validators for one field

users can set MaxDbList and MaxTableList, values are bounded by the original value
This commit is contained in:
Crack
2010-07-17 14:02:19 +02:00
parent afca401bc8
commit a7fe3f362c
6 changed files with 116 additions and 42 deletions

View File

@@ -224,7 +224,20 @@ var validators = {
// convert PCRE regexp
var parts = regexp.match(validators._regexp_pcre_extract);
var valid = this.value.match(new RegExp(parts[2], parts[3])) != null;
return valid ? true : PMA_messages['error_invalid_value']
return valid ? true : PMA_messages['error_invalid_value'];
},
/**
* Validates upper bound for numeric inputs
*
* @param {boolean} isKeyUp
* @param {int} max_value
*/
validate_upper_bound: function(isKeyUp, max_value) {
var val = parseInt(this.value);
if (isNaN(val)) {
return true;
}
return val <= max_value ? true : PMA_messages['error_value_lte'].replace('%s', max_value);
},
// field validators
_field: {

View File

@@ -125,16 +125,16 @@ $cfg_db['_overrides']['Servers/1/extension'] = extension_loaded('mysqli')
$cfg_db['_validators'] = array(
'CharTextareaCols' => 'validate_positive_number',
'CharTextareaRows' => 'validate_positive_number',
'DefaultPropDisplay' => array('validate_by_regex', '/^(?:horizontal|vertical|\d+)$/'),
'DefaultPropDisplay' => array(array('validate_by_regex', '/^(?:horizontal|vertical|\d+)$/')),
'ExecTimeLimit' => 'validate_non_negative_number',
'Export/sql_max_query_size' => 'validate_positive_number',
'ForeignKeyMaxLimit' => 'validate_positive_number',
'Import/csv_enclosed' => array('validate_by_regex', '/^.?$/'),
'Import/csv_escaped' => array('validate_by_regex', '/^.$/'),
'Import/csv_terminated' => array('validate_by_regex', '/^.$/'),
'Import/ldi_enclosed' => array('validate_by_regex', '/^.?$/'),
'Import/ldi_escaped' => array('validate_by_regex', '/^.$/'),
'Import/ldi_terminated' => array('validate_by_regex', '/^.$/'),
'Import/csv_enclosed' => array(array('validate_by_regex', '/^.?$/')),
'Import/csv_escaped' => array(array('validate_by_regex', '/^.$/')),
'Import/csv_terminated' => array(array('validate_by_regex', '/^.$/')),
'Import/ldi_enclosed' => array(array('validate_by_regex', '/^.?$/')),
'Import/ldi_escaped' => array(array('validate_by_regex', '/^.$/')),
'Import/ldi_terminated' => array(array('validate_by_regex', '/^.$/')),
'Import/skip_queries' => 'validate_non_negative_number',
'InsertRows' => 'validate_positive_number',
'LeftFrameTableLevel' => 'validate_positive_number',
@@ -145,7 +145,7 @@ $cfg_db['_validators'] = array(
'MaxCharactersInDisplayedSQL' => 'validate_positive_number',
'MaxRows' => 'validate_positive_number',
'MaxTableList' => 'validate_positive_number',
'MemoryLimit' => array('validate_by_regex', '/^\d+(?:[kmg])?$/i'),
'MemoryLimit' => array(array('validate_by_regex', '/^\d+(?:[kmg])?$/i')),
'QueryHistoryMax' => 'validate_positive_number',
'QueryWindowWidth' => 'validate_positive_number',
'QueryWindowHeight' => 'validate_positive_number',
@@ -157,4 +157,11 @@ $cfg_db['_validators'] = array(
'TextareaCols' => 'validate_positive_number',
'TextareaRows' => 'validate_positive_number',
'TrustedProxies' => 'validate_trusted_proxies');
/**
* Additional validators used for user preferences
*/
$cfg_db['_userValidators'] = array(
'MaxDbList' => array(array('validate_upper_bound', 'value:MaxDbList')),
'MaxTableList' => array(array('validate_upper_bound', 'value:MaxTableList')));
?>

View File

@@ -83,8 +83,10 @@ class FormDisplay
'error_nan_p' => __('Not a positive number'),
'error_nan_nneg' => __('Not a non-negative number'),
'error_incorrect_port' => __('Not a valid port number'),
'error_invalid_value' => __('Incorrect value')
);
'error_invalid_value' => __('Incorrect value'),
'error_value_lte' => __('Value must be equal or lower than %s'));
// initialize validators
PMA_config_get_validators();
}
/**
@@ -183,7 +185,7 @@ class FormDisplay
$js = array();
$js_default = array();
$tabbed_form = $tabbed_form && (count($this->forms) > 1);
$validators = ConfigFile::getInstance()->getDbEntry('_validators', array());
$validators = PMA_config_get_validators();
display_form_top();
@@ -654,4 +656,4 @@ class FormDisplay
return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);
}
}
?>
?>

View File

@@ -343,20 +343,22 @@ function display_form_bottom()
/**
* Appends JS validation code to $js_array
*
* @param string $field_id
* @param string $validator
* @param array $js_array
* @param string $field_id
* @param string|array $validator
* @param array $js_array
*/
function js_validate($field_id, $validator, &$js_array)
function js_validate($field_id, $validators, &$js_array)
{
$validator = (array)$validator;
$v_name = array_shift($validator);
$v_args = array();
foreach ($validator as $arg) {
$v_args[] = PMA_escapeJsString($arg);
foreach ((array)$validators as $validator) {
$validator = (array)$validator;
$v_name = array_shift($validator);
$v_args = array();
foreach ($validator as $arg) {
$v_args[] = PMA_escapeJsString($arg);
}
$v_args = $v_args ? ", ['" . implode("', '", $v_args) . "']" : '';
$js_array[] = "validateField('$field_id', '$v_name', true$v_args)";
}
$v_args = $v_args ? ", ['" . implode("', '", $v_args) . "']" : '';
$js_array[] = "validateField('$field_id', '$v_name', true$v_args)";
}
/**

View File

@@ -18,7 +18,9 @@ $forms['Features']['General'] = array(
'InitialSlidersState',
'ErrorIconic',
'ReplaceHelpImg',
'SkipLockedTables');
'SkipLockedTables',
'MaxDbList',
'MaxTableList');
$forms['Features']['Text_fields'] = array(
'CharEditing',
'CharTextareaCols',

View File

@@ -15,6 +15,43 @@
* @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
*/
/**
* Returns validator list
*
* @uses ConfigFile::getDbEntry()
* @uses ConfigFile::getInstance()
* @return array
*/
function PMA_config_get_validators()
{
static $validators = null;
if ($validators === null) {
$cf = ConfigFile::getInstance();
$validators = $cf->getDbEntry('_validators', array());
if (!defined('PMA_SETUP') || !PMA_SETUP) {
$uvs = $cf->getDbEntry('_userValidators', array());
foreach ($uvs as $field => $uv_list) {
$uv_list = (array)$uv_list;
foreach ($uv_list as &$uv) {
if (!is_array($uv)) {
continue;
}
for ($i = 1; $i < count($uv); $i++) {
if (substr($uv[$i], 0, 6) == 'value:') {
$uv[$i] = PMA_array_read(substr($uv[$i], 6), $GLOBALS['cfg']);
}
}
}
$validators[$field] = isset($validators[$field])
? array_merge((array)$validators[$field], $uv_list)
: $uv_list;
}
}
}
return $validators;
}
/**
* Runs validation $validator_id on values $values and returns error list.
*
@@ -24,6 +61,9 @@
* cleanup in HTML documen
* o false - when no validators match name(s) given by $validator_id
*
* @uses ConfigFile::getCanonicalPath()
* @uses ConfigFile::getInstance()
* @uses PMA_config_get_validators()
* @param string|array $validator_id
* @param array $values
* @param bool $isPostSource tells whether $values are directly from POST request
@@ -32,10 +72,10 @@
function PMA_config_validate($validator_id, &$values, $isPostSource)
{
// find validators
$cf = ConfigFile::getInstance();
$validator_id = (array) $validator_id;
$validators = $cf->getDbEntry('_validators');
$validators = PMA_config_get_validators();
$vids = array();
$cf = ConfigFile::getInstance();
foreach ($validator_id as &$vid) {
$vid = $cf->getCanonicalPath($vid);
if (isset($validators[$vid])) {
@@ -59,23 +99,25 @@ function PMA_config_validate($validator_id, &$values, $isPostSource)
// validate
$result = array();
foreach ($vids as $vid) {
// call appropriate validation function
$vdef = (array) $validators[$vid];
$vname = array_shift($vdef);
$args = array_merge(array($vid, &$arguments), $vdef);
$r = call_user_func_array($vname, $args);
// call appropriate validation functions
foreach ((array)$validators[$vid] as $validator) {
$vdef = (array) $validator;
$vname = array_shift($vdef);
$args = array_merge(array($vid, &$arguments), $vdef);
$r = call_user_func_array($vname, $args);
// merge results
if (is_array($r)) {
foreach ($r as $key => $error_list) {
// skip empty values if $isPostSource is false
if (!$isPostSource && empty($error_list)) {
continue;
// merge results
if (is_array($r)) {
foreach ($r as $key => $error_list) {
// skip empty values if $isPostSource is false
if (!$isPostSource && empty($error_list)) {
continue;
}
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] = array_merge($result[$key], (array)$error_list);
}
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] = array_merge($result[$key], (array)$error_list);
}
}
}
@@ -157,6 +199,7 @@ function test_db_connection($extension, $connect_type, $host, $port, $socket, $u
/**
* Validate server config
*
* @uses test_db_connection()
* @param string $path
* @param array $values
* @return array
@@ -191,6 +234,7 @@ function validate_server($path, $values)
/**
* Validate pmadb config
*
* @uses test_db_connection()
* @param string $path
* @param array $values
* @return array
@@ -229,6 +273,7 @@ function validate_pmadb($path, $values)
/**
* Validates regular expression
*
* @uses test_php_errormsg()
* @param string $path
* @param array $values
* @return array
@@ -331,6 +376,7 @@ function test_number($path, $values, $allow_neg, $allow_zero, $max_value, $error
/**
* Validates port number
*
* @uses test_number()
* @param string $path
* @param array $values
* @return array
@@ -343,6 +389,7 @@ function validate_port_number($path, $values)
/**
* Validates positive number
*
* @uses test_number()
* @param string $path
* @param array $values
* @return array
@@ -355,6 +402,7 @@ function validate_positive_number($path, $values)
/**
* Validates non-negative number
*
* @uses test_number()
* @param string $path
* @param array $values
* @return array