22
votes

In a form, I have some inputText with two commandButton, one to accept and one to cancel.

How can I disable validation for the cancel button only?

<h:form id="detailsForm">
  <p:inputText id="editUsername" value="#{userController.editUser.usrUsername}" />
  <p:inputText id="editFirstName" value="#{userController.editUser.usrFirstName}" />
  <p:inputText id="editLastName" value="#{userController.editUser.usrLastName}" />
  <p:commandButton value="Accept" update=":detailsForm" actionListener="#{userController.onDetailsEditAccept}" />
  <p:commandButton value="Cancel" update=":detailsForm" actionListener="#{userController.onDetailsEditCancel}" />
</h:form>

I already tried inserting required="false" on fields but it didn't work. I also tried inserting <f:validateBean disabled="true" /> on fields and it didn't work.

3

3 Answers

34
votes

Use the attribute immediate="true" in your cancel commandButton. This will skip the entire processing of the form, tough, by skipping the Apply Request Values, Process Validations and Update Model Values phases.

<p:commandButton value="Cancel" update=":detailsForm" actionListener="#{userController.onDetailsEditCancel}" immediate="true"/>
15
votes

Use the attribute process="@this" in the Cancel button. This will prevent the whole form being submitted.

Or you can use p:button instead (however this doesn't have the actionListener attribute). See this other Q/A

0
votes

if You don't want to submit the form. You should use just the <p:button>. the <p:commandButton> submits the form.

<p:button value="delete All"
action="#{reloadBean.purge}" update="@form"/>