public class V2120Validator extends BaseValidator { private static final int bit_of_chinese = 2; @Override protected void validate(Controller c) { if("add".equals(c.getPara("type"))){ validateLen("telNo", 11, "msg", "请输入正确的手机号!"); validateId("telNo", "select * from sms_user where telno=", "msg", "电话号码已经存在!"); validateLen("name", 20, "msg", "输入的姓名超出了长度限制!"); validateLen("dept", 20, "msg", "输入的部门名称超出了长度限制!"); }else { validateLen("telNo", 11, "msg", "请输入正确的手机号!"); validateLen("name", 20, "msg", "输入的姓名超出了长度限制!"); validateLen("dept", 20, "msg", "输入的部门名称超出了长度限制!"); } } /** * 验证电话手机号码11位 * * @param field * @param errorKey * @param errorMsg */ protected void validateId(String field, String sql, String errorKey, String errorMsg) { try { String value = controller.getPara(field); String sqlStr = sql + "'" + value + "'"; Record record = Db.findFirst(sqlStr); if (record != null) { addError(errorKey, errorMsg); return; } } catch (Exception e) { addError(errorKey, field + "验证失败!"); } } protected void validateLen(String field, int len, String errorKey, String errorMsg) { try { String value = controller.getPara(field); if (value == null || value.trim().equals("")) { } else { int l = getStrLength(value); if (l > len) { addError(errorKey, errorMsg); return; } } } catch (Exception e) { addError(errorKey, field + "验证失败!"); } } protected int getStrLength(String s) { if (s == null || s.trim().equals("")) { return 0; } s = s.trim(); int l = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) > 255) l = l + bit_of_chinese; else l = l + 1; } return l; } @Override protected void handleError(Controller c) { c.renderJson(); } }
自己定义的验证方法,不能短路!请问如何实现短路,验证一个错误后,直接返回?