mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-10 04:03:01 +09:00
issue 74: Implement conditional statement in rulesets
git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8582 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
e0c298d958
commit
1e70e0240b
4 changed files with 107 additions and 10 deletions
|
|
@ -83,6 +83,16 @@ class Validator
|
|||
$name = $filter['name'];
|
||||
unset($filter['name']);
|
||||
|
||||
// conditional statement
|
||||
if(isset($field->if)) {
|
||||
$if = $field->if;
|
||||
if(!is_array($if)) $if = array($if);
|
||||
foreach($if as $idx=>$cond) {
|
||||
$if[$idx] = (array)$cond->attrs;
|
||||
}
|
||||
$filter['if'] = $if;
|
||||
}
|
||||
|
||||
$filters[$name] = $filter;
|
||||
}
|
||||
|
||||
|
|
@ -117,17 +127,31 @@ class Validator
|
|||
if(!is_array($fields)) return true;
|
||||
|
||||
$filter_default = array(
|
||||
'required' => 'false',
|
||||
'default' => '',
|
||||
'modifiers' => array(),
|
||||
'length' => 0,
|
||||
'equalto' => 0,
|
||||
'rule' => 0
|
||||
'rule' => 0,
|
||||
'if' => array()
|
||||
);
|
||||
|
||||
$fields = array_map('trim', $fields);
|
||||
|
||||
foreach($this->_filters as $key=>$filter) {
|
||||
$exists = array_key_exists($key, $fields);
|
||||
$value = $exists?trim($fields[$key]):null;
|
||||
$filter = array_merge($filter_default, $filter);
|
||||
$value = $exists ? $fields[$key] : null;
|
||||
|
||||
// conditional statement
|
||||
foreach($filter['if'] as $cond) {
|
||||
if(!isset($cond['test']) || !isset($cond['attr'])) continue;
|
||||
|
||||
$func_body = preg_replace('/\\$(\w+)/', '$c[\'$1\']', $cond['test']);
|
||||
$func = create_function('$c', "return !!({$func_body});");
|
||||
|
||||
if($func($fields)) $filter[$cond['attr']] = $cond['value'];
|
||||
}
|
||||
|
||||
// attr : default
|
||||
if(!$value && strlen($default=trim($filter['default']))) {
|
||||
|
|
@ -240,7 +264,18 @@ class Validator
|
|||
else $args = array($name=>$filter);
|
||||
|
||||
foreach($args as $name=>$filter) {
|
||||
if($filter) $this->_filters[$name] = $filter;
|
||||
if(!$filter) continue;
|
||||
|
||||
if(isset($filter['if'])) {
|
||||
if(is_array($filter['if']) && count($filter['if'])) {
|
||||
$key = key($filter['if']);
|
||||
if(!is_int($key)) $filter['if'] = array($filter['if']);
|
||||
} else {
|
||||
unset($filter['if']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_filters[$name] = $filter;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -348,6 +383,13 @@ class Validator
|
|||
if($min) $field[] = "minlength:'{$min}'";
|
||||
if($max) $field[] = "maxlength:'{$max}'";
|
||||
}
|
||||
if($filter['if']) {
|
||||
$ifs = array();
|
||||
foreach($ifs as $if) {
|
||||
$ifs[] = "{test:'{$if['test']}', attr:'{$if['attr']}', value:'{$if['value']}'}";
|
||||
}
|
||||
$field[] = "'if':[".implode(',', $ifs)."]";
|
||||
}
|
||||
if(count($field)) {
|
||||
$field = '{'.implode(',', $field).'}';
|
||||
$content[] = "v.addFilter('{$name}', {$field});";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue