I’m new to JSF and I’m sure I’m doing something stupid, but I’ve been trying different things for days and cant make any progress. I’m trying to do validation when a user types in a date instead of using the rich calendar but for some reason I cant seem to get the validator to fire. The page code is as follows:
<a4j:outputPanel id="responseReleaseDate" rendered="#{appealSearchManager.isVendor}">
<p><h:outputText value="#{messages.ResponseReleaseDate}"/></p>
<rich:calendar id="responseReleaseDateBegin" datePattern="MM/dd/yyyy"
enableManualInput="true"
buttonIcon="/images/calendar_icon.jpg" buttonClass="calendar"
validator="#{appealSearchManager.validateResponseReleaseDateBegin}"
value="#{appealSearchManager.responseReleaseDateBegin}">
</rich:calendar>
<rich:calendar id="responseReleaseDateEnd" datePattern="MM/dd/yyyy"
enableManualInput="true"
buttonIcon="/images/calendar_icon.jpg" buttonClass="calendar"
validator="#{appealSearchManager.validateResponseReleaseDateBegin}"
value="#{appealSearchManager.responseReleaseDateEnd}">
</rich:calendar>
</a4j:outputPanel>
The bean code I’m trying to invoke is as follows:
public void validateResponseReleaseDateBegin(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
if (value != null && !value.equals("")) {
try {
simpleDateFormat.parse(value.toString());
} catch (ParseException e) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR,
MessageFormat.format((RootUtils.getCommonBundle().getString(BundleConstants.INVALID_ITEM)), "Response Release Date"),
MessageFormat.format(RootUtils.getCommonBundle().getString(BundleConstants.INVALID_DATE_FORMAT), "Date", "MM/DD/YYYY")));
}
}
}
The wierd thing is that I can reach the validateResponseReleaseDateBegin(…) method using the code below (I know, it doesnt make sense for a text field, it was just for testing purposes). But I never hit the method when I enter input for the rich calendar.
<div class="div30">
<p><h:outputText value="#{messages.ProgramInvoiceId}"/></p>
<h:inputText id="programInvoiceId"
validator="#{appealSearchManager.validateResponseReleaseDateBegin}"
value="#{appealSearchManager.programInvoiceId}"/>
</div>
Any ideas why this validator works in one place and not another?
Thanks!
This makes no sense. The
<rich:calendar>will already implicitly convert theStringsubmitted value toDatebefore setting it as model value. If you enter a date in invalid format, the<rich:calendar>will already throw aConverterExceptionfor that. Its message should already be visible in any<h:messages>or<h:message>component associated with the component.As conversion runs before validation, your validator is never fired when conversion fails. Even when your validator was fired, the
Object valueargument in the validator is already of the typejava.util.Date. So if your validator was fired, it would always have thrown an exception becauseDate#toString()does definitely not matchMM/dd/yyyy.I’m not sure why you need this validator. Perhaps you just wanted to supply a custom conversion error message? In that case, you should be using its
converterMessageattribute instead.Or, if you really need to “validate” the date format by yourself, implement a custom
Converterinstead and register it byconverterattribute or<f:converter>tag instead.