Minimal example dialog:
<p:dialog header="Test Dialog"
widgetVar="testDialog">
<h:form>
<p:inputText value="#{mbean.someValue}"/>
<p:commandButton value="Save"
onsuccess="testDialog.hide()"
actionListener="#{mbean.saveMethod}"/>
</h:form>
</p:dialog>
What I want to be able to do is have the mbean.saveMethod somehow prevent the dialog from closing if there was some problem and only output a message through growl. This is a case where a validator won’t help because there’s no way to tell if someValue is valid until a save is submitted to a back end server. Currently I do this using the visible attribute and point it to a boolean field in mbean. That works but it makes the user interface slower because popping up or down the dialog requires hitting the server.
The
onsuccessruns if ajax request itself was successful (i.e. there’s no network error, uncaught exception, etc), not if action method was successfully invoked.Given a
<p:dialog widgetVar="yourWidgetVarName">, you could remove theonsuccessand replace it by PrimeFacesRequestContext#execute()insidesaveMethod():Note:
PF()was introduced in PrimeFaces 4.0. In older PrimeFaces versions, you needyourWidgetVarName.hide()instead.If you prefer to not clutter the controller with view-specific scripts, you could use
oncompleteinstead which offers anargsobject which has a booleanvalidationFailedproperty:The
if (args)check is necessary because it may be absent when an ajax error has occurred and thus cause a new JS error when you try to getvalidationFailedfrom it; the&instead of&is mandatory for the reason explained in this answer, refactor if necessary to a JS function which you invoke likeoncomplete="hideDialogOnSuccess(args, 'yourWidgetVarName')"as shown in Keep <p:dialog> open when validation has failed.If there is however no validation error and the action method is successfully triggered, and you would still like to keep the dialog open because of e.g. an exception in the service method call, then you can manually trigger
validationFailedtotruefrom inside backing bean action method by explicitly invokingFacesContext#validationFailed(). E.g.