You can use the visible <p:dialog> attribute to indicate whether the dialog will be displayed by default or not. You can check FacesContext#isValidationFailed() if validation fails or not.
So, in a nutshell:
<p:dialog id="msgDialog" widgetVar="messageDialog" modal="true" appendToBody="true" visible="#{facesContext.validationFailed}"> <p:messages id="messagesInDialog" /> <p:button value="OK" onclick="messageDialog.hide()" /> </p:dialog>
(note that I simplified the unnecessary h:form and p:commandButton by p:button )
which then needs to be updated:
<p:commandButton value="submit" update=":msgDialog" />
Or just placing it inside <p:outputPanel autoUpdate="true"> so that it automatically updates for every ajax request without having to specify it in each update attribute:
<p:outputPanel autoUpdate="true"> <p:dialog id="msgDialog" widgetVar="messageDialog" modal="true" appendToBody="true" visible="#{facesContext.validationFailed}"> <p:messages id="messagesInDialog" /> <p:button value="OK" onclick="messageDialog.hide()" /> </p:dialog> </p:outputPanel>
See also:
Unrelated to a specific problem, to cover non-audit messages such as those global messages that you add in the action method, rather check instead of FacesContext#getMessageList() not empty.
<p:dialog ... visible="#{not empty facesContext.messageList}">
Then a dialog box will appear when any message appears. Therefore, calling RequestContext#execute() not needed.
Balusc
source share