The onsuccess
runs 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 the onsuccess
and replace it by PrimeFaces RequestContext#execute()
inside saveMethod()
:
if (success) {
RequestContext.getCurrentInstance().execute("PF('yourWidgetVarName').hide()");
}
Note: PF()
was introduced in PrimeFaces 4.0. In older PrimeFaces versions, you need yourWidgetVarName.hide()
instead.
If you prefer to not clutter the controller with view-specific scripts, you could use oncomplete
instead which offers an args
object which has a boolean validationFailed
property:
<p:commandButton ...
oncomplete="if (args && !args.validationFailed) PF('yourWidgetVarName').hide()" />
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 get validationFailed
from it; the &
instead of &
is mandatory for the reason explained in this answer, refactor if necessary to a JS function which you invoke like oncomplete="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 validationFailed
to true
from inside backing bean action method by explicitly invoking FacesContext#validationFailed()
. E.g.
FacesContext.getCurrentInstance().validationFailed();