Vincent
Vincent

Reputation: 480

Primefaces cellEditor required inputText

I have a PrimeFaces p:dataTable with one column of type p:cellEditor with an p:inputText field.

This inputText is supposed to be required but if I set required="true" and leave the input empty nothing happens / no message is shown. The form including the above is submitted anyway.

Here's my code, especially the <p:column headerText="Data Value"> part:

<h:form id="my-form">
            <p:dataTable var="dataItem" rowIndexVar="rowIndex" value="#{backingBean.dataList}" draggableRows="true" editable="true" editMode="cell">
                    <p:ajax event="rowReorder" update="@form"/>

                        <p:column headerText="Order Position">
                            <h:outputText value="#{rowIndex+1}" />
                        </p:column>
                        <p:column headerText="Data Name">
                            <h:outputText value="#{dataItem.name}" />
                        </p:column>

                        <p:column headerText="Data Value">
                            <p:cellEditor>
                                <f:facet name="output"><h:outputText value="#{dataItem.value}" /></f:facet>
                                <f:facet name="input"><p:inputText required="true" requiredMessage="Please insert a value" id="valueInput" value="#{dataItem.value}" style="width:100%"/></f:facet>
                            </p:cellEditor>
                        </p:column>
                </p:dataTable>
            <div>
                <h:commandLink action="#{backingBean.submit()}">Submit</h:commandLink>
            </div>
            <h:messages id="validateMsg" />
        </h:form>

The submitting h:commandLink is not an ajax call. But in my opinion it shouldn't submit the form anyway as long as the required p:inputText fields are empty.

I also tried using h:inputText instead of p:inputText with the same result.

I tried to add a required h:inputText outside the p:dataTable which works as expected and prevents the form from being submitted as long as it is empty.

Any ideas about how I get a required p:inputText field in p:cellEdit ?

Upvotes: 1

Views: 3861

Answers (1)

Vincent
Vincent

Reputation: 480

According to rion18's suggestion. I'm checking in my backing bean if all values are set. Only if all values are set the submit button is shown. Here's how I ended up doing this:

<h:form id="my-form">
    <p:dataTable var="dataItem" rowIndexVar="rowIndex" value="#{backingBean.dataList}" draggableRows="true" editable="true" editMode="cell">
    <p:ajax event="cellEdit" update=":#{p:component('submit-button-group')}" />
        <p:column>
...
        </p:column>
    </p:dataTable>
    <div>
        <h:panelGroup id="submit-button-group">
            <h:commandLink action="#{backingBean.submit()}" rendered="#{backingBean.isAllValuesSet()}">Submit</h:commandLink>
        </h:panelGroup>
    </div>
</h:form>

In my backing bean I have a simple function that iterates over the dataList:

public boolean isAllValuesSet(){
    for(DataItem item:dataItems){
        if(item.getValue()==null || item.getValue().isEmpty()){
            return false;
        }
    }
    return true;
}

Upvotes: 1

Related Questions