1
votes

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 ?

1
Not sure but try adding something like <p:ajax event="rowEdit" update=":growl" /> (replace ":growl" with your messages-component) - Jaqen H'ghar
I also have inconsistent results with @form. Use @this or directly reference the table id in the p:ajax - kolossus
Thank you for your answers! I tried changing the @form update to id values and also used the ajax event 'cellEdit' but it does not work either. The more bad thing besides the message not shown is the form being submitted although the required fields are empty. - Vincent
2 ideas: 1) How about building a custom validator? 2) If the problem is the submitting part, how about disabling the submit link until every one of the inputs are not null (or empty)? - rion18
Thank you rion18. Your second suggestion is how I finally ended up doing it. - Vincent

1 Answers

0
votes

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;
}