Reputation: 15
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!
Upvotes: 1
Views: 6652
Reputation: 1108782
This makes no sense. The <rich:calendar>
will already implicitly convert the String
submitted value to Date
before setting it as model value. If you enter a date in invalid format, the <rich:calendar>
will already throw a ConverterException
for 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 value
argument in the validator is already of the type java.util.Date
. So if your validator was fired, it would always have thrown an exception because Date#toString()
does definitely not match MM/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 converterMessage
attribute instead.
<rich:calendar ... converterMessage="Invalid date" />
Or, if you really need to "validate" the date format by yourself, implement a custom Converter
instead and register it by converter
attribute or <f:converter>
tag instead.
Upvotes: 5