Usually the PrimeFaces tag for messages shows the global as well as the field specific messages. What I want to do is to just show the global messages (globalOnly=\"tr
I use a phase listener to do so. This is roughly my implementation (using OmniFaces):
public class ValidationFailedListener implements PhaseListener {
@Override
public void afterPhase(PhaseEvent event) {
// NOOP
}
@Override
public void beforePhase(PhaseEvent event) {
if (Faces.isValidationFailed()) {
Messages.addGlobalError("Your validation failed message");
}
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}
You should register it in your faces-config.xml
:
<lifecycle>
<phase-listener>your.ValidationFailedListener</phase-listener>
</lifecycle>
My actual implementation uses a message bundle to display a localized message.
If you cannot use OmniFaces, here is the relevant code using vanilla JSF:
if (FacesContext.getCurrentInstance().isValidationFailed()) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Your validation failed message",
null);
FacesContext.getCurrentInstance().addMessage(null, message);
}