Пример #1
0
  public ValidatorConfigBean create(String fieldName, ValidatorConfigBean val) {
    if (this.ann == null) return null;
    if (val == null || !Validators.ENUM.equals(val.getName())) {
      val = new ValidatorConfigBean();
      val.setName(Validators.ENUM);
    }

    FieldConfigBean fcb = new FieldConfigBean();
    fcb.setName(fieldName);
    fcb.setMessage(CommonUtil.parsePropValue(ann.mess()));
    ParamConfigBean pcb = new ParamConfigBean();
    pcb.setName(Validators.ENUM_WORD_PARAM);
    StringBuilder sb = new StringBuilder();
    for (String s : ann.words()) {
      if (sb.length() > 0) sb.append("#");

      sb.append(CommonUtil.parsePropValue(s));
    }

    pcb.setValue(sb.toString());
    fcb.getParam().add(pcb);

    val.getField().add(fcb);

    return val;
  }
  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();
      String value = context.getQueryParamMap().get(key)[0]; // 这个是封装了request请求传过来的参数map
      if (value.indexOf("中国") != -1) errMap.put(key, fcb.getMessage());
    }

    Validation validation = new Validation();
    validation.getErrors().put(val.getName(), errMap);

    return validation;
  }