How to execute a4j:commandLink oncomplete only when validation has succeed?

d-man picture d-man · May 28, 2012 · Viewed 13.2k times · Source

I am using JSF2, Richfaces4 and Spring. I have a command link. On execute I save records and on complete I execute search to display records like following code. Problem is that fireSearch() in oncomplete executes even if there is a validation error in form. I need to execute fireSearch() only when the record get saved successfully.

How can I the oncomplete only when validation has succeed?

<a4j:commandLink styleClass="button" action="#{myBean.save}" render="detail_form" execute="@form" oncomplete="fireSearch()">
<span> Save </span>
</a4j:commandLink>

Answer

BalusC picture BalusC · May 28, 2012

RichFaces supports request based EL evaluation in oncomplete attribute. So you should be able to print FacesContext#isValidationFailed() as if it's a JS condition.

For example,

oncomplete="if (#{!facesContext.validationFailed}) fireSearch()"

If validation has failed, this will ultimately result in

oncomplete="if (false) fireSearch()"

And thus the function won't be invoked.