issue 18 : Fix a bug for empty error messages

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8471 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
taggon 2011-06-10 02:21:34 +00:00
parent 7107bc0d77
commit 8502d9cae3

View file

@ -141,7 +141,7 @@ class Validator
if(is_string($modifiers=$filter['modifiers'])) $modifiers = explode(',', trim($modifiers)); if(is_string($modifiers=$filter['modifiers'])) $modifiers = explode(',', trim($modifiers));
// attr : required // attr : required
if($filter['required'] === 'true' && !$value_len) return $this->error($key, ''); if($filter['required'] === 'true' && !$value_len) return $this->error($key, 'isnull');
// if the field wasn't passed, ignore this value // if the field wasn't passed, ignore this value
if(!$exists && !$value_len) continue; if(!$exists && !$value_len) continue;
@ -158,7 +158,7 @@ class Validator
$strlength = $this->_has_mb_func?mb_strlen($value,'utf-8'):$this->mbStrLen($value); $strlength = $this->_has_mb_func?mb_strlen($value,'utf-8'):$this->mbStrLen($value);
} }
if($min > ($is_min_b?$strbytes:$strlength) || $max < ($is_max_b?$strbytes:$strlength)) return $this->error($key, 'length'); if($min > ($is_min_b?$strbytes:$strlength) || $max < ($is_max_b?$strbytes:$strlength)) return $this->error($key, 'outofrange');
} }
// equalto // equalto
@ -166,12 +166,15 @@ class Validator
if(!array_key_exists($equalto, $fields) || trim($fields[$equalto]) !== $value) return $this->error($key, 'equalto'); if(!array_key_exists($equalto, $fields) || trim($fields[$equalto]) !== $value) return $this->error($key, 'equalto');
} }
// rule // rules
if($rule=$filter['rule']){ if($rules=$filter['rule']){
$result = $this->applyRule($rule, $value); $rules = explode(',', $rules);
// apply the 'not' modifier foreach($rules as $rule) {
if(in_array('not', $modifiers)) $result = !$result; $result = $this->applyRule($rule, $value);
if(!$result) return $this->error($key, 'rule error'); // apply the 'not' modifier
if(in_array('not', $modifiers)) $result = !$result;
if(!$result) return $this->error($key, 'invalid_'.$rule);
}
} }
} }
@ -184,6 +187,11 @@ class Validator
* @return always false * @return always false
*/ */
function error($field, $msg){ function error($field, $msg){
global $lang;
$msg = isset($lang->filter->{$msg})?$lang->filter->{$msg}:$lang->filter->invalid;
$msg = sprintf($msg, $field);
$this->_last_error = array('field'=>$field, 'msg'=>$msg); $this->_last_error = array('field'=>$field, 'msg'=>$msg);
return false; return false;