📄 fieldchecks.java.svn-base
字号:
float max = Float.parseFloat(maxVar); if (min > max) { throw new IllegalArgumentException(sysmsgs.getMessage( "invalid.range", minVar, maxVar)); } if (!GenericValidator.isInRange(floatValue, min, max)) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } } catch (Exception e) { processFailure(errors, field, validator.getFormName(), "floatRange", e); return false; } return true; } /** * Checks if the field is a valid credit card number. * * @param bean The bean validation is being performed on. * @param va The <code>ValidatorAction</code> that is currently * being performed. * @param field The <code>Field</code> object associated with the * current field being validated. * @param errors The <code>ActionMessages</code> object to add errors * to if any validation errors occur. * @param validator The <code>Validator</code> instance, used to access * other field values. * @param request Current request object. * @return true if valid, false otherwise. */ public static Object validateCreditCard(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { Object result = null; String value = null; try { value = evaluateBean(bean, field); } catch (Exception e) { processFailure(errors, field, validator.getFormName(), "creditCard", e); return Boolean.FALSE; } if (GenericValidator.isBlankOrNull(value)) { return Boolean.TRUE; } result = GenericTypeValidator.formatCreditCard(value); if (result == null) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); } return (result == null) ? Boolean.FALSE : result; } /** * Checks if a field has a valid e-mail address. * * @param bean The bean validation is being performed on. * @param va The <code>ValidatorAction</code> that is currently * being performed. * @param field The <code>Field</code> object associated with the * current field being validated. * @param errors The <code>ActionMessages</code> object to add errors * to if any validation errors occur. * @param validator The <code>Validator</code> instance, used to access * other field values. * @param request Current request object. * @return True if valid, false otherwise. */ public static boolean validateEmail(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { String value = null; try { value = evaluateBean(bean, field); } catch (Exception e) { processFailure(errors, field, validator.getFormName(), "email", e); return false; } if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } else { return true; } } /** * Checks if the field's length is less than or equal to the maximum * value. A <code>Null</code> will be considered an error. * * @param bean The bean validation is being performed on. * @param va The <code>ValidatorAction</code> that is currently * being performed. * @param field The <code>Field</code> object associated with the * current field being validated. * @param errors The <code>ActionMessages</code> object to add errors * to if any validation errors occur. * @param validator The <code>Validator</code> instance, used to access * other field values. * @param request Current request object. * @return True if stated conditions met. */ public static boolean validateMaxLength(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { String value = null; try { value = evaluateBean(bean, field); if (value != null) { String maxVar = Resources.getVarValue("maxlength", field, validator, request, true); int max = Integer.parseInt(maxVar); boolean isValid = false; String endLth = Resources.getVarValue("lineEndLength", field, validator, request, false); if (GenericValidator.isBlankOrNull(endLth)) { isValid = GenericValidator.maxLength(value, max); } else { isValid = GenericValidator.maxLength(value, max, Integer.parseInt(endLth)); } if (!isValid) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } } catch (Exception e) { processFailure(errors, field, validator.getFormName(), "maxlength", e); return false; } return true; } /** * Checks if the field's length is greater than or equal to the minimum * value. A <code>Null</code> will be considered an error. * * @param bean The bean validation is being performed on. * @param va The <code>ValidatorAction</code> that is currently * being performed. * @param field The <code>Field</code> object associated with the * current field being validated. * @param errors The <code>ActionMessages</code> object to add errors * to if any validation errors occur. * @param validator The <code>Validator</code> instance, used to access * other field values. * @param request Current request object. * @return True if stated conditions met. */ public static boolean validateMinLength(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { String value = null; try { value = evaluateBean(bean, field); if (!GenericValidator.isBlankOrNull(value)) { String minVar = Resources.getVarValue("minlength", field, validator, request, true); int min = Integer.parseInt(minVar); boolean isValid = false; String endLth = Resources.getVarValue("lineEndLength", field, validator, request, false); if (GenericValidator.isBlankOrNull(endLth)) { isValid = GenericValidator.minLength(value, min); } else { isValid = GenericValidator.minLength(value, min, Integer.parseInt(endLth)); } if (!isValid) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } } catch (Exception e) { processFailure(errors, field, validator.getFormName(), "minlength", e); return false; } return true; } /** * Checks if a field has a valid url. Four optional variables can be * specified to configure url validation. * * <ul> * * <li>Variable <code>allow2slashes</code> can be set to <code>true</code> * or <code>false</code> to control whether two slashes are allowed - * default is <code>false</code> (i.e. two slashes are NOT allowed).</li> * * <li>Variable <code>nofragments</code> can be set to <code>true</code> * or <code>false</code> to control whether fragments are allowed - * default is <code>false</code> (i.e. fragments ARE allowed).</li> * * <li>Variable <code>allowallschemes</code> can be set to * <code>true</code> or <code>false</code> to control if all schemes are * allowed - default is <code>false</code> (i.e. all schemes are NOT * allowed).</li> * * <li>Variable <code>schemes</code> can be set to a comma delimited list * of valid schemes. This value is ignored if <code>allowallschemes</code> * is set to <code>true</code>. Default schemes allowed are "http", * "https" and "ftp" if this variable is not specified.</li> * * </ul> * * @param bean The bean validation is being performed on. * @param va The <code>ValidatorAction</code> that is currently * being performed. * @param field The <code>Field</code> object associated with the * current field being validated. * @param errors The <code>ActionMessages</code> object to add errors * to if any validation errors occur. * @param validator The <code>Validator</code> instance, used to access * other field values. * @param request Current request object. * @return True if valid, false otherwise. */ public static boolean validateUrl(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { String value = null; try { value = evaluateBean(bean, field); } catch (Exception e) { processFailure(errors, field, validator.getFormName(), "url", e); return false; } if (GenericValidator.isBlankOrNull(value)) { return true; } // Get the options and schemes Vars String allowallschemesVar = Resources.getVarValue("allowallschemes", field, validator, request, false); boolean allowallschemes = "true".equalsIgnoreCase(allowallschemesVar); int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0; String allow2slashesVar = Resources.getVarValue("allow2slashes", field, validator, request, false); if ("true".equalsIgnoreCase(allow2slashesVar)) { options += UrlValidator.ALLOW_2_SLASHES; } String nofragmentsVar = Resources.getVarValue("nofragments", field, validator, request, false); if ("true".equalsIgnoreCase(nofragmentsVar)) { options += UrlValidator.NO_FRAGMENTS; } String schemesVar = allowallschemes ? null : Resources.getVarValue("schemes", field, validator, request, false); // No options or schemes - use GenericValidator as default if ((options == 0) && (schemesVar == null)) { if (GenericValidator.isUrl(value)) { return true; } else { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } // Parse comma delimited list of schemes into a String[] String[] schemes = null; if (schemesVar != null) { StringTokenizer st = new StringTokenizer(schemesVar, ","); schemes = new String[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { schemes[i++] = st.nextToken().trim(); } } // Create UrlValidator and validate with options/schemes UrlValidator urlValidator = new UrlValidator(schemes, options); if (urlValidator.isValid(value)) { return true; } else { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } /** * Process a validation failure. */ private static void processFailure(ActionMessages errors, Field field, String formName, String validatorName, Throwable t) { // Log the error String logErrorMsg = sysmsgs.getMessage("validation.failed", validatorName, field.getProperty(), formName, t.toString()); log.error(logErrorMsg, t); // Add general "system error" message to show to the user String userErrorMsg = sysmsgs.getMessage("system.error"); errors.add(field.getKey(), new ActionMessage(userErrorMsg, false)); } /** * Return <code>true</code> if the specified object is a String or a * <code>null</code> value. * * @param o Object to be tested * @return The string value */ protected static boolean isString(Object o) { return (o == null) ? true : String.class.isInstance(o); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -