primefaces rowEdit closes after validationFailed()

1.3k Views Asked by At

I am using Primefaces 4.0 and JSF 2.2. When I make a DataTable with row Edit and I set a valdiationFailed() on the rowEdit event, the roweditor is closing, which I want to prevent.

I added an oncomplete js function like:

<p:ajax event="rowEdit" listener="#{customerUI.onInvoiceRowEdit}" 
   oncomplete="if(!args.validationFailed) {updateTable();}" update=":messages" />

My remote command is as follows:

<p:remoteCommand name="updateTable" update=":form:addressTabs:customerTable" />

So this keeps the editor when the validation fails, but now the editors accept and cancel buttons doesn't work, so does editing other things on the side until i do a manual refresh.

I just want the editor to stay when the validation fails and to correct the input, if the validation went good, the editor can be closed.

Anyone any solution to this?

1

There are 1 best solutions below

0
On

So I had a tangentially related problem and this question helped me figure out a solution so I thought I should share it here in case someone else lands on this question with a similar issue and needs help.

So I had a dataTable and a rowEdit ajax call to a function when a cell within the row is edited, I also used a remoteCommand to update the dataTable after I make some changes in the DB. Problem was if the validation for the cell edit value failed the remoteCommand would refresh the form and the error messages would disappear before they could be read. I wanted the ajax call on rowEdit to only go through if there were no validation errors, otherwise I wanted to display the error message with the row editor still open. So I used the args.validationFailed parameter as shown in the question to prevent the remoteCommand from being executed in case the custom validator fails. The following is a basic example of what I did.

JSF page:-

<h:form id="form_name">
    <p:remoteCommand name="refreshForm" update="@form" />

    <p:dataTable id="table_id" var="varTableData"
        value="#{bean.tablerows}"
        editable="true">

        <p:ajax event="rowEdit"
            listener="#{bean.onRowEdit}"
            oncomplete="if(!args.validationFailed) refreshForm()" />

        <p:column>
            <f:facet name="header">
                <h:outputText value="Column Title" />
            </f:facet>
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{varTableData.someValue}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText id="cellId"
                        value="#{varTableData.someValue}"
                        label="Some Label" required="true" style="width:100%"
                        validator="#{bean.validationMethod}">
                    </p:inputText>
                    <p:message id="someMsg" for="cellId" />
                </f:facet>
            </p:cellEditor>
        </p:column>
    </p:dataTable>
</h:form>

Custom Validation Method :-

public void validationMethod(FacesContext context, UIComponent comp, Object value) {
    int someCellValue = (int) value;
    
    if (someCellValue < 6) {
        ((UIInput) comp).setValid(false);

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                "Some error message",
                "some error message");
        context.addMessage(comp.getClientId(context), message);
    }
}