rym
rym

Reputation: 545

How to validate a date from a Calendar

I have tried the following code (I have followed an example) to test a date choosen from a calendar. If the date don't exist in my database, then a validation error message should be shown to the enduser. However, the error does not end up in the <h:message>, instead it is being logged to the server log.

View:

<p:calendar id="date1" value="#{bean.date1}" showOn="button"> 
    <f:validator validatorId="calendarValidator" />
    <f:ajax execute="date1" render="calendarmessage" />
</p:calendar>
<h:message id="calendarmessage" for="date1" />

Validator:

@FacesValidator("calendarValidator")
public class CalendarValidator implements Validator{

    @Override  
    public void validate(FacesContext context, UIComponent component, Object value) {
        java.util.Date date2 = (java.util.Date) value;

        try {
            if (validateDate(date2)) {
                throw new ValidatorException(new FacesMessage("A valid date"));
            } else {
                throw new ValidatorException(new FacesMessage("date dont figure in the database"));
            } 
        } catch(Exception e) {
            e.printStackTrace();
        }   
    }
}

Server log:

INFO: date invalide
GRAVE: javax.faces.validator.ValidatorException: date dont figure in the database
at DAOKPI.CalendarValidator.validate(CalendarValidator.java:60)
at javax.faces.component.UIInput.validateValue(UIInput.java:1149)
at javax.faces.component.UIInput.validate(UIInput.java:967)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1

Upvotes: 4

Views: 14067

Answers (2)

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76828

Aside from the problem in the validator that @Balusc already mentions in his answer, you should use PrimeFaces-components consitently:

<p:calendar id="date1" value="#{bean.date1}" showOn="button"> 
    <f:validator validatorId="calendarValidator" />
    <p:ajax execute="date1" update="calendarmessage" />
</p:calendar>
<p:message id="calendarmessage" for="date1" />

Upvotes: 1

BalusC
BalusC

Reputation: 1108782

Look at your code once again, you're catching and printing the ValidatorException inside the validator yourself! You should not catch and log the ValidatorException yourself. You should let it go and let JSF catch and handle it so that it can display it in the message component accordingly.

Get rid of the try block in the validator and add the throws clause to the validate() method.

@Override  
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    java.util.Date date2 = (java.util.Date) value;

    if (validateDate(date2)) {
        throw new ValidatorException(new FacesMessage("A valid date")); // Btw: I wonder how that makes sense... That's actually not a validation error.
    } else {
        throw new ValidatorException(new FacesMessage("date dont figure in the database"));
    } 
}   

Upvotes: 4

Related Questions