fareed
fareed

Reputation: 3082

Primefaces validate cell editing

I'm using cell editor in Primefaces to update cells in a datatable. However I want to validate the input before I confirm the change.

I have used FacesContext.getCurrentInstance().validationFailed(); for this purpose but still getting the cell updated.

This is how I'm implementing it:

<p:dataTable value="#{bean.list}" var="var" id="table" editMode="cell" editable="true">
    <p:ajax event="cellEdit" listener="#{bean.onCellEdit}" update="@form"/>
    <p:column headerText="Quantity">
        <p:cellEditor>
            <f:facet name="output">
                 <h:outputText value="#{var.quantity}"/>
            </f:facet>  
            <f:facet name="input">
                   <p:inputText value="#{var.quantity}"/>
             </f:facet>
        </p:cellEditor>
    </p:column>
</p:dataTable>

Bean Method:

public void onCellEdit(CellEditEvent event) {
    //validate new value
     if(!validate(event.getNewValue())){
        //if validation returned false stop updating the cell
        FacesContext.getCurrentInstance().validationFailed();
    }
}

I want to stop updating the cell if the new value did not pass the validation, but the cell gets updated anyways. How can I solve this problem?

PS: Primefaces 3.5

Upvotes: 2

Views: 5426

Answers (1)

kolossus
kolossus

Reputation: 20691

What you're observing happens because because the celleditor's event happens in the INVOKE_APPLICATION phase, too late for any validation failure to have any effect.


You can just use the plain validator attribute on the <p:inputText/> like you would any other JSF input component. The behaviour will be the same, regardless of the fact that it's a facet of the <p:cellEditor/>

Upvotes: 2

Related Questions