Can anyone help, the below code gets executed properly in my debug mode, however it does show the error message to the screen. It works fine for all other errors except this validator plugin. Could anyone tell me what's wrong with my code below:
validation-rule.xml:
<validator name="dateRange"
classname="ca.cn.ems.web.forms.DateRangeValidator"
method="validateTwoDate"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest,
javax.servlet.ServletContext"
depends=""
msg="error.invalid.daterange"/>
validation.xml:
<form-validation>
<formset>
<form name="equipmentForm">
<field property="beginDate" depends="required,date">
<arg key="form.beginDate" />
<var>
<var-name>datePatternStrict</var-name>
<var-value>yyyy-MM-dd</var-value>
</var>
</field>
<field property="endDate" depends="required,date,dateRange">
<arg key="form.endDate" />
<var>
<var-name>datePatternStrict</var-name>
<var-value>yyyy-MM-dd</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
Java:
public static boolean validateTwoDate(java.lang.Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("enter DateRangeValidator.validateTwoDate");
}
String endDate = ValidatorUtils.getValueAsString(bean, field
.getProperty());
String beginDate = ValidatorUtils.getValueAsString(bean, "beginDate");
Date startDateRet = CommonUtil.getDate(beginDate);
Date endDateRet = CommonUtil.getDate(endDate);
if (!GenericValidator.isBlankOrNull(endDate)) {
try {
if (endDateRet.before(startDateRet)) {
errors.add(field.getKey(),
Resources.getActionError(request, va, field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionError(request, va, field));
return false;
}
}
return true;
}
Conclusion I think that the problem seems to be the java code, could anyone tell me what I'm doing wrong :
errors.add(field.getKey(), Resources.getActionError(request, va, field));
return false;