Automatically show validation messages in the p dialog: if validation fails

I want to present verification messages and messages from bean support in the <p:dialog> component. On my JSF page, I defined the following dialog:

 <p:dialog widgetVar="messageDialog" id="msgDialog" modal="true" appendToBody="true"> <h:form id="messageForm"> <p:messages id="messagesInDialog" /> <p:commandButton value="OK" onclick="messageDialog.hide()" /> </h:form> </p:dialog> 

I am executing the following code after adding some message to the bean backup:

 RequestContext.getCurrentInstance().execute("messageDialog.show()"); 

and it works great.

However, I also want to display bean verification messages in this dialog box. Messages are added to the verification of the <p:message> component, but I do not know how to display this dialog box after the verification fails.

How can I achieve this?

+7
source share
1 answer

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.

+13
source

All Articles