Fixed a bug for client-side validation

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8841 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
taggon 2011-08-23 08:57:24 +00:00
parent 1548d0a54b
commit 8c8f3dc298
3 changed files with 27 additions and 26 deletions

View file

@ -220,7 +220,7 @@
$matches[1] = preg_replace('/'.$m[0].'/i', '', $matches[1]);
$matches[2] = '<input type="hidden" name="ruleset" value="'.$m[1].'" />'.$matches[2];
if(preg_match('@(?:^|/)(modules/[\w-]+)@', $this->path, $mm)) {
if(preg_match('@(?:^|\.?/)(modules/[\w-]+)@', $this->path, $mm)) {
$module_path = $mm[1];
$validator = new Validator("{$module_path}/ruleset/{$m[1]}.xml");
$validator->setCacheDir('files/cache');

View file

@ -34,6 +34,7 @@ class Validator
));
$this->_has_mb_func = is_callable('mb_strlen');
$this->setCacheDir('./files/cache');
}
/**
@ -99,6 +100,8 @@ class Validator
$this->_xml_ruleset = $xml->ruleset;
$this->_filters = $filters;
$this->_xml_path = $xml_path;
return true;
}
/**
@ -116,7 +119,7 @@ class Validator
* @param[in] (optional) array $fields Target fields. The keys of the array represents field's name, its values represents field's value.
* @return bool True if it is valid, FALSE otherwise.
*/
function validate($fields_=null){
function validate($fields_=array()){
if(is_array($fields_)) {
$fields = $fields_;
} else {
@ -124,7 +127,7 @@ class Validator
$fields = (array)Context::getRequestVars();
}
if(!is_array($fields)) return true;
if(!is_array($fields) || !count($fields)) return true;
$filter_default = array(
'required' => 'false',
@ -343,6 +346,8 @@ class Validator
fclose($fp);
}
}
return $filepath;
}
/**
@ -350,6 +355,7 @@ class Validator
* @private
*/
function _compile2js() {
$ruleset = basename($this->_xml_path,'.xml');
$content = array();
// custom rulesets
@ -357,14 +363,14 @@ class Validator
if(strpos('email,userid,url,alpha,alpha_number,number,', $name.',') !== false) continue;
switch($rule['type']) {
case 'regex':
$content[] = "v.addRule('{$name}', {$rule['test']});";
$content[] = "v.cast('ADD_RULE', ['{$name}', {$rule['test']}]);";
break;
case 'enum':
$enums = '"'.implode('","', $rule['test']).'"';
$content[] = "v.addRule('{$name}', function($$){ return ($.inArray($$,[{$enums}]) > -1); });";
$content[] = "v.cast('ADD_RULE', ['{$name}', function($$){ return ($.inArray($$,[{$enums}]) > -1); }]);";
break;
case 'expr':
$content[] = "v.addRule('{$name}', function($$){ return ({$rule['test']}); });";
$content[] = "v.cast('ADD_RULE', ['{$name}', function($$){ return ({$rule['test']}); }]);";
break;
}
}
@ -392,19 +398,14 @@ class Validator
}
if(count($field)) {
$field = '{'.implode(',', $field).'}';
$content[] = "v.addFilter('{$name}', {$field});";
$content[] = "'{$name}':{$field}";
}
}
if(count($content)) {
array_unshift($content,
'(function($){',
'var v = xe.getApp("validator")[0];',
'if(!v) return false;'
);
$content[] = '})(jQuery);'; // array_push
$content = implode(',', $content);
return implode("\n", $content);
return "(function($,v){\nv=xe.getApp('validator')[0];if(!v)return;\nv.cast('ADD_FILTER',['{$ruleset}', {{$content}}]);})(jQuery);";
} else {
return '';
}