Tarun Sapra
Tarun Sapra

Reputation: 1901

How to check in after phase of Validations phase if validation has failed?

I have written a PhaseListener in which I am checking for the Validations phase. Is there a way using which I can check in the afterPhase() method of listener that validation has failed and the next phase after the Validation phase will be Render Response phase.

Upvotes: 0

Views: 6587

Answers (3)

BalusC
BalusC

Reputation: 1109745

If you're on JSF 2.0, just use FacesContext#isValidationFailed() to check it.

if (context.isValidationFailed()) {
    // Validation has failed.
}

You can by the way also check for it in the view side as follows:

<h:panelGroup rendered="#{facesContext.validationFailed}">
    <p>Validation has failed.</p>
</h:panelGroup>

Upvotes: 6

prajeesh kumar
prajeesh kumar

Reputation: 1966

Use the maximum severity for error in the FacesContext

Severity maximumSeverity = FacesContext.getCurrentInstance().getMaximumSeverity();
boolean validationFailed=false;
if (maximumSeverity != null
        && (maximumSeverity==FacesMessage.SEVERITY_ERROR || maximumSeverity
            ==FacesMessage.SEVERITY_FATAL)) {
    validationFailed=true;
}

If the value of the validationFailed is true then there are some error messages present.

Upvotes: 3

Kris
Kris

Reputation: 5792

How about using FacesContext, can you check if there are any enqueued messages? Eg.

FacesContext context = FacesContext.getCurrentInstance();
Iterator<FacesMessage> messages = context.getMessages();

hope that helps.

Upvotes: 1

Related Questions