/** * Validates that two fields match. * * @param bean * @param va * @param field * @param errors * @param request * @return boolean */ public static boolean validateTwoFields( Object bean, ValidatorAction va, Field field, ActionMessages errors, HttpServletRequest request) { String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); String sProperty2 = field.getVarValue("secondProperty"); String value2 = ValidatorUtils.getValueAsString(bean, sProperty2); if (!GenericValidator.isBlankOrNull(value)) { try { if (!value.equals(value2)) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } } catch (Exception e) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } } return true; }
/** * Extracts the value of the given bean. If the bean is <code>null</code>, the returned value is * also <code>null</code>. If the bean is a <code>String</code> then the bean itself is returned. * In all other cases, the <code>ValidatorUtils</code> class is used to extract the bean value * using the <code>Field</code> object supplied. * * @see ValidatorUtils#getValueAsString(Object, String) */ protected static String extractValue(Object bean, Field field) { String value = null; if (bean == null) { return null; } else if (bean instanceof String) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } return value; }
public static boolean validate( Object bean, ValidatorAction va, Field field, ActionMessages errors, HttpServletRequest request, ServletContext application) { String valueString = ValidatorUtils.getValueAsString(bean, field.getProperty()); String sProperty2 = ValidatorUtils.getValueAsString(bean, field.getVarValue("month")); String sProperty3 = ValidatorUtils.getValueAsString(bean, field.getVarValue("day")); if (((valueString == null) && (sProperty2 == null) && (sProperty3 == null)) || ((valueString.length() == 0) && (sProperty2.length() == 0) && (sProperty3.length() == 0))) { // errors.add(field.getKey(),Resources.getActionError(request, va, // field)); return true; } Integer year = null; Integer month = null; Integer day = null; try { year = new Integer(valueString); month = new Integer(sProperty2); day = new Integer(sProperty3); } catch (NumberFormatException e) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } if (!GenericValidator.isBlankOrNull(valueString)) { if (!Data.validDate(day, month, year) || year == null || month == null || day == null || year.intValue() < 1 || month.intValue() < 0 || day.intValue() < 1) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); } return false; } return true; }
// this validator is only valid when used in year field public static boolean threeArgsDate( Object bean, ValidatorAction va, Field field, ActionMessages errors, HttpServletRequest request, ServletContext application) { String valueString1 = ValidatorUtils.getValueAsString(bean, field.getProperty()); String sProperty2 = ValidatorUtils.getValueAsString(bean, field.getVarValue("month")); String sProperty3 = ValidatorUtils.getValueAsString(bean, field.getVarValue("day")); if (((valueString1 == null) && (sProperty2 == null) && (sProperty3 == null)) || ((valueString1.length() == 0) && (sProperty2.length() == 0) && (sProperty3.length() == 0))) { // errors.add(field.getKey(),Resources.getActionError(request, va, // field)); return true; } Integer year = null; Integer month = null; Integer day = null; try { year = new Integer(valueString1); month = new Integer(sProperty2); day = new Integer(sProperty3); } catch (NumberFormatException e) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } String date = new String(day.toString() + "/" + month.toString() + "/" + year); String datePattern = "dd/MM/yyyy"; if (!GenericValidator.isDate(date, datePattern, false)) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } return true; }
/** * Validates that two fields match. * * @param bean * @param va * @param field * @param errors */ public static boolean validateTwoFields( Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); String sProperty2 = field.getVarValue("secondProperty"); String value2 = ValidatorUtils.getValueAsString(bean, sProperty2); if (!GenericValidator.isBlankOrNull(value)) { try { if (!value.equals(value2)) { FieldChecks.rejectValue(errors, field, va); return false; } } catch (Exception e) { FieldChecks.rejectValue(errors, field, va); return false; } } return true; }
/** * Gets the <code>ActionMessage</code> based on the <code>ValidatorAction</code> message and the * <code>Field</code>'s arg objects. * * @param validator the Validator * @param request the servlet request * @param va Validator action * @param field the validator Field */ public static ActionMessage getActionMessage( Validator validator, HttpServletRequest request, ValidatorAction va, Field field) { Msg msg = field.getMessage(va.getName()); if (msg != null && !msg.isResource()) { return new ActionMessage(msg.getKey(), false); } String msgKey = null; String msgBundle = null; if (msg == null) { msgKey = va.getMsg(); } else { msgKey = msg.getKey(); msgBundle = msg.getBundle(); } if (msgKey == null || msgKey.length() == 0) { return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false); } ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM); MessageResources messages = getMessageResources(application, request, msgBundle); Locale locale = RequestUtils.getUserLocale(request, null); Arg[] args = field.getArgs(va.getName()); String[] argValues = getArgValues(application, request, messages, locale, args); ActionMessage actionMessage = null; if (msgBundle == null) { actionMessage = new ActionMessage(msgKey, argValues); } else { String message = messages.getMessage(locale, msgKey, argValues); actionMessage = new ActionMessage(message, false); } return actionMessage; }
/** * Gets the <code>Locale</code> sensitive value based on the key passed in. * * @param application the servlet context * @param request the servlet request * @param defaultMessages The default Message resources * @param locale The locale * @param va The Validator Action * @param field The Validator Field */ public static String getMessage( ServletContext application, HttpServletRequest request, MessageResources defaultMessages, Locale locale, ValidatorAction va, Field field) { Msg msg = field.getMessage(va.getName()); if (msg != null && !msg.isResource()) { return msg.getKey(); } String msgKey = null; String msgBundle = null; MessageResources messages = defaultMessages; if (msg == null) { msgKey = va.getMsg(); } else { msgKey = msg.getKey(); msgBundle = msg.getBundle(); if (msg.getBundle() != null) { messages = getMessageResources(application, request, msg.getBundle()); } } if (msgKey == null || msgKey.length() == 0) { return "??? " + va.getName() + "." + field.getProperty() + " ???"; } // Get the arguments Arg[] args = field.getArgs(va.getName()); String[] argValues = getArgValues(application, request, messages, locale, args); // Return the message return messages.getMessage(locale, msgKey, argValues); }
/** * Validates that two fields match. * * @param bean * @param va * @param field * @param errors */ public static boolean validateTwoFields( Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); String sProperty2 = field.getVarValue("equalTo"); String value2 = ValidatorUtils.getValueAsString(bean, sProperty2); if (value == null && value2 == null) { return true; } else if (value != null && value2 == null) { return false; } else if (value == null && value2 != null) { return false; } try { if (!value.equals(value2)) { FieldChecks.rejectValue(errors, field, va); return false; } } catch (Exception e) { FieldChecks.rejectValue(errors, field, va); return false; } return true; }
/** * Checks if the field matches the boolean expression specified in <code>test</code> parameter. * * @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 request Current request object. * @return <code>true</code> if meets stated requirements, <code>false</code> otherwise. */ public static boolean validateValidWhen( Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { Object form = validator.getParameterValue(Validator.BEAN_PARAM); String value = null; boolean valid = false; int index = -1; if (field.isIndexed()) { String key = field.getKey(); final int leftBracket = key.indexOf("["); final int rightBracket = key.indexOf("]"); if ((leftBracket > -1) && (rightBracket > -1)) { index = Integer.parseInt(key.substring(leftBracket + 1, rightBracket)); } } if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } String test = null; try { test = Resources.getVarValue("test", field, validator, request, true); } catch (IllegalArgumentException ex) { String logErrorMsg = sysmsgs.getMessage( "validation.failed", "validwhen", field.getProperty(), validator.getFormName(), ex.toString()); log.error(logErrorMsg); String userErrorMsg = sysmsgs.getMessage("system.error"); errors.add(field.getKey(), new ActionMessage(userErrorMsg, false)); return false; } // Create the Lexer ValidWhenLexer lexer = null; try { lexer = new ValidWhenLexer(new StringReader(test)); } catch (Exception ex) { String logErrorMsg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex; log.error(logErrorMsg); String userErrorMsg = sysmsgs.getMessage("system.error"); errors.add(field.getKey(), new ActionMessage(userErrorMsg, false)); return false; } // Create the Parser ValidWhenParser parser = null; try { parser = new ValidWhenParser(lexer); } catch (Exception ex) { String logErrorMsg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex; log.error(logErrorMsg); String userErrorMsg = sysmsgs.getMessage("system.error"); errors.add(field.getKey(), new ActionMessage(userErrorMsg, false)); return false; } parser.setForm(form); parser.setIndex(index); parser.setValue(value); try { parser.expression(); valid = parser.getResult(); } catch (Exception ex) { String logErrorMsg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex; log.error(logErrorMsg); String userErrorMsg = sysmsgs.getMessage("system.error"); errors.add(field.getKey(), new ActionMessage(userErrorMsg, false)); return false; } if (!valid) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } return true; }