22

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.

www.jensolsson.se
  • 3,023
  • 2
  • 35
  • 65
Paolo Dragone
  • 939
  • 1
  • 11
  • 27

3 Answers3

34

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"/>
Fritz
  • 9,987
  • 4
  • 30
  • 49
15

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

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117
0

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"/>
zedtimi
  • 306
  • 1
  • 6