use of org.eweb4j.mvc.action.Validation in project eweb4j-framework by laiweiwei.
the class ValidateExecution method execute.
public static Validation execute(Context context) {
Validation validation = new Validation();
List<ValidatorConfigBean> valList = context.getActionConfigBean().getValidator();
if (valList == null || valList.isEmpty())
return validation;
// 启动验证器
for (int i = valList.size() - 1; i > -1; i--) {
ValidatorConfigBean val = valList.get(i);
ValidatorIF validator = null;
if (!"".equals(val.getName())) {
// 根据name属性实例化相关的验证器,如果找不到,就根据class属性实例化
validator = ValidatorFactory.getValidator(val.getName());
if (validator == null)
try {
validator = (ValidatorIF) Thread.currentThread().getContextClassLoader().loadClass(val.getClazz()).newInstance();
} catch (Exception e) {
log.error(e.toString(), e);
}
}
if (validator == null)
continue;
Map<String, Map<String, String>> err = validator.validate(val, context).getErrors();
for (Entry<String, Map<String, String>> en : err.entrySet()) {
String key = en.getKey();
Map<String, String> value = en.getValue();
if (validation.getErrors().containsKey(key))
validation.getErrors().get(key).putAll(value);
else
validation.getErrors().put(key, value);
}
}
return validation;
}
use of org.eweb4j.mvc.action.Validation in project eweb4j-framework by laiweiwei.
the class TestValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> errMap = new HashMap<String, String>();
for (FieldConfigBean fcb : val.getField()) {
// 比如我现在验证 "name"参数值不能出现 "中国" 字眼
String key = fcb.getName();
// 这个是封装了request请求传过来的参数map
String value = context.getQueryParamMap().get(key)[0];
if (value.indexOf("中国") != -1)
errMap.put(key, fcb.getMessage());
}
Validation validation = new Validation();
validation.getErrors().put(val.getName(), errMap);
return validation;
}
use of org.eweb4j.mvc.action.Validation in project eweb4j-framework by laiweiwei.
the class DateValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> valError = new HashMap<String, String>();
for (FieldConfigBean f : val.getField()) {
String[] value = context.getQueryParamMap().get(f.getName());
if (value == null || value.length == 0)
continue;
String mess = f.getMessage();
param: for (ParamConfigBean p : f.getParam()) {
if (!Validators.DATE_FORMAT_PARAM.equalsIgnoreCase(p.getName()))
continue;
String paramValue = p.getValue();
mess = mess.replace("{format}", paramValue);
DateFormat df = new SimpleDateFormat(paramValue);
for (String v : value) {
try {
df.parse(v);
} catch (Exception e) {
valError.put(f.getName(), mess);
break param;
}
}
}
context.getRequest().setAttribute(f.getName(), value);
}
Validation validation = new Validation();
if (!valError.isEmpty())
validation.getErrors().put(val.getName(), valError);
return validation;
}
use of org.eweb4j.mvc.action.Validation in project eweb4j-framework by laiweiwei.
the class ForbidWordValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> valError = new HashMap<String, String>();
for (FieldConfigBean f : val.getField()) {
String[] value = context.getQueryParamMap().get(f.getName());
if (value == null || value.length == 0)
continue;
String mess = f.getMessage();
param: for (ParamConfigBean p : f.getParam()) {
if (Validators.FORBID_WORD_PARAM.equalsIgnoreCase(p.getName())) {
String[] forbidWord = p.getValue().split("#");
if (forbidWord == null || forbidWord.length == 0)
continue;
boolean flag = false;
for (String v : value) {
for (String word : forbidWord) {
if (v.indexOf(word) != -1) {
flag = true;
break;
}
}
if (!flag)
continue;
valError.put(f.getName(), mess.replace("{words}", p.getValue()));
break param;
}
}
}
context.getRequest().setAttribute(f.getName(), value);
}
Validation validation = new Validation();
if (!valError.isEmpty())
validation.getErrors().put(val.getName(), valError);
return validation;
}
use of org.eweb4j.mvc.action.Validation in project eweb4j-framework by laiweiwei.
the class IntegerSizeValidator method validate.
public Validation validate(ValidatorConfigBean val, Context context) {
Map<String, String> valError = new HashMap<String, String>();
for (FieldConfigBean f : val.getField()) {
String[] value = context.getQueryParamMap().get(f.getName());
if (value == null || value.length == 0)
continue;
String mess = f.getMessage();
int min = 0;
int max = 0;
boolean minFlag = false;
boolean maxFlag = false;
for (ParamConfigBean p : f.getParam()) {
if (Validators.MIN_SIZE_PARAM.equals(p.getName())) {
min = Integer.parseInt(p.getValue());
minFlag = true;
} else if (Validators.MAX_SIZE_PARAM.equals(p.getName())) {
max = Integer.parseInt(p.getValue());
maxFlag = true;
}
if (minFlag && maxFlag)
break;
}
for (String v : value) {
if (!v.matches(RegexList.integer_regexp))
continue;
int iv = Integer.parseInt(v);
mess = mess.replace("{min}", min + "").replace("{max}", max + "");
if (iv < min) {
valError.put(f.getName(), mess);
break;
} else if (iv > max) {
valError.put(f.getName(), mess);
break;
}
}
context.getRequest().setAttribute(f.getName(), value);
}
Validation validation = new Validation();
if (!valError.isEmpty())
validation.getErrors().put(val.getName(), valError);
return validation;
}
Aggregations