0

I'm using primefaces.

I have a form and a dataTable with a lazyModel, in the dataTable I show the results found after searching with the input data.

I need to do a couple of validations in my bean side whenever I press the "Search" button, and in the case where a validation fails I don't want to re-render my dataTable because if I do it, then it will launch a new query to the database with the wrong input values and it will lose the previous results.

<h:form>
  <!-- Here I have a bunch of inputTexts -->
  <h:commandButton value="Search">
        <p:ajax process="@form" update="@form" listener="#{myBean.search}"/>
  </h:commandButton>

  <p:dataTable id="myResults" var="item" value="#{myBean.lazyModel}" lazy="true">
       <!-- Rows of my dataTable -->
  </p:dataTable>
<h:form>

In my bean I have something like the next code (of course it is more complicated than that, otherwise I will create a JSF validator:

public void search() {
    // test validation
    if (myService.validateData(this.value1, this.value2, this.value3) and this.value4 == false) {
        MuMessageUtils.setFacesErrorMessage("validation.error.1");
        // I don't want to refresh my dataTable
    }
    // everything ok
    else {
        // I want to refresh my dataTable

        // do more stuff with the data
    }
}

Can I detect whenever the validation fails and stop the update process from the ajax event attached to the Search button, or even not process at all the form if the validation fails?

Thanks.

maqjav
  • 2,310
  • 3
  • 23
  • 35

1 Answers1

1

You can use PrimeFaces RequestContext to update programmatically in your listener method. The method will be called only if validation succeeded, since the Invoke Application phase is skipped if validation failed.

import org.primefaces.context.RequestContext;

RequestContext.getCurrentInstance().update("form_client_id");

Update your inputs in p:ajax so the failed input fields would get highlighted .

<p:ajax process="@form" update="inputs" listener="#{myBean.search}"/>

("inputs" here is for demonstrative purposes, I don't know what to specify there since the actual inputs are not posted in the question.)

Vsevolod Golovanov
  • 4,068
  • 3
  • 31
  • 65
  • Thanks for your answer. I suppose this could work, instead of defining what part of my page I don't want to render, I define what part I do want to render. I'm going to test it and I will give you a feedback. – maqjav Jun 03 '15 at 11:44
  • I posted the answer thinking the question implied actual JSF validation. Anyway, the gist of the answer still applies. – Vsevolod Golovanov Jun 03 '15 at 12:15