mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-05 09:41:40 +09:00
Support patterns in field name
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8995 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
578d3d6b6e
commit
76999d3054
2 changed files with 77 additions and 12 deletions
|
|
@ -16,7 +16,11 @@ class Validator
|
|||
/**
|
||||
* @constructor
|
||||
*/
|
||||
function Validator($xml_path=''){
|
||||
function Validator($xml_path='') {
|
||||
$this->__construct($xml_path);
|
||||
}
|
||||
|
||||
function __construct($xml_path='') {
|
||||
$this->_rules = array();
|
||||
$this->_filters = array();
|
||||
$this->_xml_ruleset = null;
|
||||
|
|
@ -37,6 +41,11 @@ class Validator
|
|||
$this->setCacheDir('./files/cache');
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
$this->_rules = null;
|
||||
$this->_filters = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a xml file
|
||||
* @param[in] string $xml_path A file name to be loaded
|
||||
|
|
@ -119,7 +128,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_=null) {
|
||||
if(is_array($fields_)) {
|
||||
$fields = $fields_;
|
||||
} else {
|
||||
|
|
@ -127,7 +136,7 @@ class Validator
|
|||
$fields = (array)Context::getRequestVars();
|
||||
}
|
||||
|
||||
if(!is_array($fields) || !count($fields)) return true;
|
||||
if(!is_array($fields)) return true;
|
||||
|
||||
$filter_default = array(
|
||||
'required' => 'false',
|
||||
|
|
@ -140,9 +149,27 @@ class Validator
|
|||
);
|
||||
|
||||
$fields = array_map('trim', $fields);
|
||||
$field_names = implode("\t", array_keys($fields));
|
||||
$field_names = array_keys($fields);
|
||||
|
||||
$filters = $this->_filters;
|
||||
|
||||
// get field names matching patterns
|
||||
foreach($this->_filters as $key=>$filter) {
|
||||
$names = array();
|
||||
if($key{0} == '^') {
|
||||
$names = preg_grep('/^'.preg_quote(substr($key,1)).'/', $field_names);
|
||||
}
|
||||
|
||||
if(!count($names)) continue;
|
||||
|
||||
foreach($names as $name) {
|
||||
$filters[$name] = $filter;
|
||||
}
|
||||
|
||||
unset($filters[$key]);
|
||||
}
|
||||
|
||||
foreach($filters as $key=>$filter) {
|
||||
$fname = $key;
|
||||
$exists = array_key_exists($key, $fields);
|
||||
$filter = array_merge($filter_default, $filter);
|
||||
|
|
@ -183,7 +210,7 @@ class Validator
|
|||
list($min, $max) = array((int)$min, (int)$max);
|
||||
|
||||
$strbytes = strlen($value);
|
||||
if(!$is_min_b || !$is_max_b){
|
||||
if(!$is_min_b || !$is_max_b) {
|
||||
$strlength = $this->_has_mb_func?mb_strlen($value,'utf-8'):$this->mbStrLen($value);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -85,31 +85,69 @@ var Validator = xe.createApp('Validator', {
|
|||
});
|
||||
},
|
||||
API_VALIDATE : function(sender, params) {
|
||||
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 result = true, form = params[0], elems = form.elements, filter, filter_to_add, ruleset, callback;
|
||||
var fields, names, name, el, val, mod, len, lenb, max, min, maxb, minb, rules, e_el, e_val, i, c, r, if_, fn;
|
||||
|
||||
if(elems['ruleset']) filter = form.elements['ruleset'].value;
|
||||
else if(elems['_filter']) filter = form.elements['_filter'].value;
|
||||
if(!filter) return true;
|
||||
|
||||
if($.isFunction(callbacks[filter])) callback = callbacks[filter];
|
||||
filter = $.extend({}, filters[filter.toLowerCase()] || {}, extras);
|
||||
|
||||
function regex_quote(str){
|
||||
return str.replace(/([\.\+\-\[\]\{\}\(\)\\])/g, '\\$1');
|
||||
};
|
||||
|
||||
// get form names
|
||||
fields = [];
|
||||
for(i=0,c=form.elements.length; i < c; i++) {
|
||||
el = elems[i];
|
||||
name = el.name;
|
||||
|
||||
if(!elems[name].length || elems[name][0] === el) fields.push(name);
|
||||
};
|
||||
fields = fields.join('\n');
|
||||
|
||||
// get field names matching patterns
|
||||
filter_to_add = {};
|
||||
for(name in filter) {
|
||||
if(!filter.hasOwnProperty(name)) continue;
|
||||
|
||||
names = [];
|
||||
if(name.substr(0,1) == '^') {
|
||||
names = fields.match( (new RegExp('^'+regex_quote(name.substr(1))+'.*$','gm')) );
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if(!names) names = [];
|
||||
|
||||
for(i=0,c=names.length; i < c; i++) {
|
||||
filter_to_add[names[i]]= filter[name];
|
||||
}
|
||||
|
||||
filter[name] = null;
|
||||
delete filter[name];
|
||||
};
|
||||
|
||||
filter = $.extend(filter, filter_to_add);
|
||||
|
||||
for(name in filter) {
|
||||
if(!filter.hasOwnProperty(name)) continue;
|
||||
|
||||
f = filter[name];
|
||||
el = form.elements[name];
|
||||
el = elems[name];
|
||||
val = el?$.trim(get_value($(el))):'';
|
||||
mod = (f.modifier||'')+',';
|
||||
|
||||
if(!el) continue;
|
||||
if(!el || el.disabled) continue;
|
||||
|
||||
if(f['if']) {
|
||||
if(!$.isArray(f['if'])) f['if'] = [f['if']];
|
||||
for(i in f['if']) {
|
||||
if_ = f['if'][i];
|
||||
fn = new Function('el', 'return !!(' + (if_.test.replace(/$(\w+)/g, 'el["$1"]')) +')');
|
||||
if(fn(form.elements)) f[if_.attr] = if_.value;
|
||||
if(fn(elems)) f[if_.attr] = if_.value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +168,7 @@ var Validator = xe.createApp('Validator', {
|
|||
}
|
||||
|
||||
if(f.equalto) {
|
||||
e_el = form.elements[f.equalto];
|
||||
e_el = elems[f.equalto];
|
||||
e_val = e_el?$.trim(get_value($(e_el))):'';
|
||||
if(e_el && e_val !== val) {
|
||||
return this.cast('ALERT', [form, name, 'equalto']) && false;
|
||||
|
|
@ -147,7 +185,7 @@ var Validator = xe.createApp('Validator', {
|
|||
return this.cast('ALERT', [form, name, 'invalid_'+r]) && false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if($.isFunction(callback)) return callback(form);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue