How to dynamically add new row to p:dataTable without validating the form?
The <p:commandButton>
submits and processes by default the entire form. This will indeed validate all input fields. You can control this with the process
attribute which thus defaults to @form
. In your particular case, you could just use @this
so that only the command button's own action is invoked.
<p:commandButton value="Add another price" process="@this" update="pricesList" action="#{productBean.addNewPrice()}" />
Related videos on Youtube
Markus Schulte
Updated on September 14, 2022Comments
-
Markus Schulte over 1 year
I'm developing a form for adding/editing product prices with JSF and PrimeFaces. A single product can have multiple prices depending on volume which is shown in a
<p:dataTable>
.The backing bean:
@ManagedBean @ViewScoped public class ProductBean { protected Product product; protected List<ProductPrice> productPrices; public void addNewPrice() { ProductPrice productPrice = new ProductPrice(); productPrice.setPrice(new BigDecimal(0)); this.productPrices.add(productPrice); } // ... }
The Facelet page:
<h:form id="productForm"> <p:inputText value="#{productBean.product.name}" required="true"> <f:ajax event="blur" render="nameMessage" /> </p:inputText> <p:message id="nameMessage" for="name" /> <p:dataTable id="pricesList" ...> </p:dataTable> <p:commandButton value="Add another price" update="pricesList" action="#{productBean.addNewPrice()}" /> <p:commandButton value="Submit" action="#{productBean.submit}" /> </h:form>
The first button "Add another price" does, what it is supposed to do: Adding a new row to "pricesList". But only if form is valid (form is invalid, if product-name is not set).
My problem is, that I am having two commandButtons for the form, but I don't know how to get my wished functionallity without a commandButton. I tried a lot of ways: Changing the "Add another price" to a standard
<p:button>
with ajax-functionality; doesn't work because of buttons' outcome. I tried "type=button" for this button, but in this case simply nothing happens.Are there any suggestions have to achieve my wished functionality? It is not necessary to have a button solving my problem.