mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-09 12:02:24 +09:00
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:
parent
1548d0a54b
commit
8c8f3dc298
3 changed files with 27 additions and 26 deletions
|
|
@ -220,7 +220,7 @@
|
||||||
$matches[1] = preg_replace('/'.$m[0].'/i', '', $matches[1]);
|
$matches[1] = preg_replace('/'.$m[0].'/i', '', $matches[1]);
|
||||||
$matches[2] = '<input type="hidden" name="ruleset" value="'.$m[1].'" />'.$matches[2];
|
$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];
|
$module_path = $mm[1];
|
||||||
$validator = new Validator("{$module_path}/ruleset/{$m[1]}.xml");
|
$validator = new Validator("{$module_path}/ruleset/{$m[1]}.xml");
|
||||||
$validator->setCacheDir('files/cache');
|
$validator->setCacheDir('files/cache');
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ class Validator
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->_has_mb_func = is_callable('mb_strlen');
|
$this->_has_mb_func = is_callable('mb_strlen');
|
||||||
|
$this->setCacheDir('./files/cache');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -99,6 +100,8 @@ class Validator
|
||||||
$this->_xml_ruleset = $xml->ruleset;
|
$this->_xml_ruleset = $xml->ruleset;
|
||||||
$this->_filters = $filters;
|
$this->_filters = $filters;
|
||||||
$this->_xml_path = $xml_path;
|
$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.
|
* @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.
|
* @return bool True if it is valid, FALSE otherwise.
|
||||||
*/
|
*/
|
||||||
function validate($fields_=null){
|
function validate($fields_=array()){
|
||||||
if(is_array($fields_)) {
|
if(is_array($fields_)) {
|
||||||
$fields = $fields_;
|
$fields = $fields_;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -124,7 +127,7 @@ class Validator
|
||||||
$fields = (array)Context::getRequestVars();
|
$fields = (array)Context::getRequestVars();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!is_array($fields)) return true;
|
if(!is_array($fields) || !count($fields)) return true;
|
||||||
|
|
||||||
$filter_default = array(
|
$filter_default = array(
|
||||||
'required' => 'false',
|
'required' => 'false',
|
||||||
|
|
@ -343,6 +346,8 @@ class Validator
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $filepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -350,6 +355,7 @@ class Validator
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function _compile2js() {
|
function _compile2js() {
|
||||||
|
$ruleset = basename($this->_xml_path,'.xml');
|
||||||
$content = array();
|
$content = array();
|
||||||
|
|
||||||
// custom rulesets
|
// custom rulesets
|
||||||
|
|
@ -357,14 +363,14 @@ class Validator
|
||||||
if(strpos('email,userid,url,alpha,alpha_number,number,', $name.',') !== false) continue;
|
if(strpos('email,userid,url,alpha,alpha_number,number,', $name.',') !== false) continue;
|
||||||
switch($rule['type']) {
|
switch($rule['type']) {
|
||||||
case 'regex':
|
case 'regex':
|
||||||
$content[] = "v.addRule('{$name}', {$rule['test']});";
|
$content[] = "v.cast('ADD_RULE', ['{$name}', {$rule['test']}]);";
|
||||||
break;
|
break;
|
||||||
case 'enum':
|
case 'enum':
|
||||||
$enums = '"'.implode('","', $rule['test']).'"';
|
$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;
|
break;
|
||||||
case 'expr':
|
case 'expr':
|
||||||
$content[] = "v.addRule('{$name}', function($$){ return ({$rule['test']}); });";
|
$content[] = "v.cast('ADD_RULE', ['{$name}', function($$){ return ({$rule['test']}); }]);";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -392,19 +398,14 @@ class Validator
|
||||||
}
|
}
|
||||||
if(count($field)) {
|
if(count($field)) {
|
||||||
$field = '{'.implode(',', $field).'}';
|
$field = '{'.implode(',', $field).'}';
|
||||||
$content[] = "v.addFilter('{$name}', {$field});";
|
$content[] = "'{$name}':{$field}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($content)) {
|
if(count($content)) {
|
||||||
array_unshift($content,
|
$content = implode(',', $content);
|
||||||
'(function($){',
|
|
||||||
'var v = xe.getApp("validator")[0];',
|
|
||||||
'if(!v) return false;'
|
|
||||||
);
|
|
||||||
$content[] = '})(jQuery);'; // array_push
|
|
||||||
|
|
||||||
return implode("\n", $content);
|
return "(function($,v){\nv=xe.getApp('validator')[0];if(!v)return;\nv.cast('ADD_FILTER',['{$ruleset}', {{$content}}]);})(jQuery);";
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
var messages = [];
|
var messages = [];
|
||||||
var rules = [];
|
var rules = [];
|
||||||
var filters = [];
|
var filters = {};
|
||||||
var callbacks = [];
|
var callbacks = [];
|
||||||
var extras = {};
|
var extras = {};
|
||||||
|
|
||||||
|
|
@ -85,10 +85,11 @@ var Validator = xe.createApp('Validator', {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
API_VALIDATE : function(sender, params) {
|
API_VALIDATE : function(sender, params) {
|
||||||
var result = true, form = params[0], filter=null, callback=null;
|
var result = true, form = params[0], elems = form.elements, filter=null, ruleset=null, callback=null;
|
||||||
var name, el, val, mod, len, lenb, max, min, maxb, minb, rules, e_el, e_val, i, c, r, result, if_, fn;
|
var name, el, val, mod, len, lenb, max, min, maxb, minb, rules, e_el, e_val, i, c, r, result, if_, fn;
|
||||||
|
|
||||||
if (form.elements['_filter']) filter = form.elements['_filter'].value;
|
if(elems['ruleset']) filter = form.elements['ruleset'].value;
|
||||||
|
else if(elems['_filter']) filter = form.elements['_filter'].value;
|
||||||
if(!filter) return true;
|
if(!filter) return true;
|
||||||
if($.isFunction(callbacks[filter])) callback = callbacks[filter];
|
if($.isFunction(callbacks[filter])) callback = callbacks[filter];
|
||||||
filter = $.extend({}, filters[filter.toLowerCase()] || {}, extras);
|
filter = $.extend({}, filters[filter.toLowerCase()] || {}, extras);
|
||||||
|
|
@ -103,11 +104,12 @@ var Validator = xe.createApp('Validator', {
|
||||||
|
|
||||||
if(!el) continue;
|
if(!el) continue;
|
||||||
|
|
||||||
if(filter['if']) {
|
if(f['if']) {
|
||||||
for(i in filter['if']) {
|
if(!$.isArray(f['if'])) f['if'] = [f['if']];
|
||||||
if_ = filter['if'][i];
|
for(i in f['if']) {
|
||||||
|
if_ = f['if'][i];
|
||||||
fn = new Function('el', 'return !!(' + (if_.test.replace(/$(\w+)/g, 'el["$1"]')) +')');
|
fn = new Function('el', 'return !!(' + (if_.test.replace(/$(\w+)/g, 'el["$1"]')) +')');
|
||||||
if(fn(form.elements)) filter[if_.attr] = if_.value;
|
if(fn(form.elements)) f[if_.attr] = if_.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -172,8 +174,6 @@ var Validator = xe.createApp('Validator', {
|
||||||
var name = params[0].toLowerCase();
|
var name = params[0].toLowerCase();
|
||||||
var filter = params[1];
|
var filter = params[1];
|
||||||
|
|
||||||
if(filter['if'] && !$.isArray(filter['if'])) filter['if'] = [filter['if']];
|
|
||||||
|
|
||||||
filters[name] = filter;
|
filters[name] = filter;
|
||||||
},
|
},
|
||||||
API_DEL_FILTER : function(sender, params) {
|
API_DEL_FILTER : function(sender, params) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue