Search in sources :

Example 1 with FieldConfigBean

use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.

the class SizeImpl method create.

public ValidatorConfigBean create(String fieldName, ValidatorConfigBean val) {
    if (this.ann == null)
        return null;
    if (val == null || !Validators.SIZE.equals(val.getName())) {
        val = new ValidatorConfigBean();
        val.setName(Validators.SIZE);
    }
    FieldConfigBean fcb = new FieldConfigBean();
    fcb.setName(fieldName);
    fcb.setMessage(CommonUtil.parsePropValue(ann.mess()));
    ParamConfigBean pcb = new ParamConfigBean();
    pcb.setName(Validators.MIN_SIZE_PARAM);
    pcb.setValue(String.valueOf(ann.min()));
    fcb.getParam().add(pcb);
    ParamConfigBean pcb2 = new ParamConfigBean();
    pcb2.setName(Validators.MAX_SIZE_PARAM);
    pcb2.setValue(String.valueOf(ann.max()));
    fcb.getParam().add(pcb2);
    val.getField().add(fcb);
    return val;
}
Also used : ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 2 with FieldConfigBean

use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.

the class ValidatorHelper method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    for (FieldConfigBean f : val.getField()) {
        String fieldName = f.getName();
        String[] value = context.getQueryParamMap().get(fieldName);
        if (value == null || value.length == 0)
            continue;
        for (String v : value) {
            if (!v.matches(this.regex)) {
                String mess = f.getMessage();
                if (mess.length() == 0)
                    mess = " %s-validator : your input { %s = %s } must matches [ %s ]";
                valError.put(fieldName, String.format(mess, val.getName(), fieldName, v, regex));
                break;
            }
        }
        context.getRequest().setAttribute(fieldName, value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 3 with FieldConfigBean

use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.

the class EnumValidator 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.ENUM_WORD_PARAM.equalsIgnoreCase(p.getName())) {
                String[] enumWord = p.getValue().split("#");
                if (enumWord == null || enumWord.length == 0)
                    continue;
                boolean flag = false;
                for (String v : value) {
                    for (String word : enumWord) {
                        if (v.equals(word)) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                        continue;
                    mess = mess.replace("{words}", p.getValue());
                    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;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 4 with FieldConfigBean

use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.

the class EqualsValidator 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();
        boolean flag = false;
        param: for (ParamConfigBean p : f.getParam()) {
            if (!Validators.EQUALS_TO_PARAM.equalsIgnoreCase(p.getName()))
                continue;
            String paramValue = p.getValue();
            String[] others = context.getQueryParamMap().get(paramValue);
            if (others == null || others.length == 0) {
                flag = true;
                break param;
            }
            for (String v : value) {
                if (!v.equals(others[0])) {
                    flag = true;
                    break param;
                }
            }
        }
        if (flag)
            valError.put(f.getName(), mess);
        context.getRequest().setAttribute(f.getName(), value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 5 with FieldConfigBean

use of org.eweb4j.mvc.config.bean.FieldConfigBean in project eweb4j-framework by laiweiwei.

the class RegexValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    for (FieldConfigBean f : val.getField()) {
        String key = f.getName();
        String[] value = context.getQueryParamMap().get(key);
        if (value == null || value.length == 0)
            continue;
        String mess = f.getMessage();
        param: for (ParamConfigBean param : f.getParam()) {
            String pName = param.getName();
            if (!Validators.REGEX_PARAM.equals(pName))
                continue;
            String pValue = param.getValue();
            for (String v : value) {
                if (!v.matches(pValue)) {
                    valError.put(key, mess.replace("{pattern}", pValue));
                    break param;
                }
            }
        }
        context.getRequest().setAttribute(key, value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Aggregations

FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)32 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)21 ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)19 HashMap (java.util.HashMap)11 Validation (org.eweb4j.mvc.action.Validation)11 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)2 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)2 UploadFile (org.eweb4j.mvc.upload.UploadFile)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)1 InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)1 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)1 DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)1