0

Let's say as for an example, I have some input fields like,

<p:panel id="panel" closable="false" toggleOrientation="horizontal" toggleable="true" header="New">
    <p:focus context="panel"/>
    <p:watermark for="txtCountryName" value="Enter a valid country name."/>
    <p:watermark for="txtCountryCode" value="Enter a valid country code."/>
    <p:messages id="systemMessages" globalOnly="true" redisplay="false" showDetail="true" showSummary="true" autoUpdate="false" closable="true"/>
    <p:messages id="specificSystemMessages" for="paramId" globalOnly="false" redisplay="false" showDetail="true" showSummary="false" autoUpdate="false" closable="true"/>

        <h:panelGrid id="panelGrid" columns="3" cellpadding="5">
            <p:outputLabel for="txtCountryName" value="Country"/>
            <p:inputText id="txtCountryName" value="#{countryManagedBean.txtCountryName}" label="Country name" required="true" maxlength="45">
                <f:validateLength minimum="2" maximum="45"/>
            </p:inputText>
            <p:message for="txtCountryName" showSummary="false"/>

            <p:outputLabel for="txtCountryCode" value="Country Code"/>
            <p:inputText id="txtCountryCode" value="#{countryManagedBean.txtCountryCode}" required="true" maxlength="45" label="Country code">
                <f:validateLength minimum="2" maximum="45"/>
            </p:inputText>
            <p:message for="txtCountryCode" showSummary="false"/>


            <p:commandButton id="btnSubmit" update="dataTable panel messages" actionListener="#{countryManagedBean.insert}" icon="ui-icon-check" value="Save"/>
</h:panelGrid>
</p:panel>

And a DataTable as follows.

<p:panel id="dataTablePanel" toggleable="true" toggleOrientation="horizontal" closable="false" header="Data">
    <p:dataTable id="dataTable" var="row" value="#{countryManagedBean}"
                 lazy="true"
                 pageLinks="10"
                 paginator="true"
                 sortMode="multiple"
                 resizableColumns="true"
                 sortOrder="descending"
                 editable="true"
                 filterEvent="keyup"
                 selection="#{countryManagedBean.selectedValues}"
                 rowsPerPageTemplate="5,10,15"
                 rows="10"
                 rowKey="#{row.countryId}"
                 rowIndexVar="rowIndex"
                 rowStyleClass="#{row.countryId eq countryManagedBean.id? 'selected-data-row' : null}"
                 editMode="row">


                 ...
                 ...
                 ...
    </p:dataTable>
</p:panel>

When this command button,

<p:commandButton id="btnSubmit" update="dataTable panel messages" actionListener="#{countryManagedBean.insert}" icon="ui-icon-check" value="Save"/>

is clicked, a row is added to the underlying database, if it satisfies all of the validation rules and the DataTable is updated using the update="dataTable panel messages" attribute.

I would like updating this DataTable if and only if all the validation criteria as specified on these input fields are satisfied and the row is actually created.

If anyone of these validation rules fails, then this DataTable should not be updated anymore which is quite unnecessary and causes some costly JPA criteria and/or JPQL queries to be executed. Is this possible, how?

It is Primefaces 3.5.

Tiny
  • 27,221
  • 105
  • 339
  • 599

2 Answers2

4

Try using remotecommand in that case. Change your submit button as shown below.

<p:commandButton id="btnSubmit" action="#{countryManagedBean.insert}" icon="ui-icon-check" value="Save" oncomplete="handleRequest(xhr, status, args)"/>

Add p:remoteCommand as shown below.

<p:remoteCommand name="updateTable" update="dataTable">

This remote command will be used for updating your table.

And yes, add below js.

<script type="text/javascript">  
        function handleRequest(xhr, status, args) {  
            if(!args.validationFailed) {  
                updateTable();
                }   
           }  
    </script> 

HTH

Pankaj Kathiriya
  • 4,210
  • 2
  • 19
  • 26
  • `panel` and `messages` along with `dataTable` should also be updated. This doesn't happen. Otherwise, it works. How to update `panel` and `messages` along with `dataTable`? – Tiny Jul 08 '13 at 09:03
  • `` doesn't update `panel` and `messages`. It only updates `dataTable` upon success (when validation succeeds). – Tiny Jul 08 '13 at 09:11
  • I am so sorry there was mistake, please use ` ` – Pankaj Kathiriya Jul 08 '13 at 09:14
  • `panel` and `messages` are ids. `messages` is an id of `` like, ``. I didn't post in the question though. – Tiny Jul 08 '13 at 09:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33069/discussion-between-pankaj-kathiriya-and-tiny) – Pankaj Kathiriya Jul 08 '13 at 09:23
  • The `process` attribute of `` defaults to `@all`. Therefore, specifying `process="@this"` with ``, in this case, is essential. Otherwise, the whole view will be processed unnecessarily, http://stackoverflow.com/a/23444159/1391249. – Tiny Oct 09 '14 at 19:30
0

try to add this code to your bakedbean countryManagedBean at the end of the insert function or whereever you check the validation.

RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("validationFailed", "true");

This creates The callback param that you check with the javascript.

  • Even easier is to explicitly update the datatable from the bean. Since that method will only be called if validation succeeds. – Kukeltje Aug 12 '16 at 09:16