29

I have a form with a validator on one field. I have two h:commandButtons: Ok and Cancel. When I input wrong data and click Cancel, I get a validation message. What must I do that validator don't run when I click cancel?

bruno
  • 2,043
  • 17
  • 26
Aram Gevorgyan
  • 2,025
  • 6
  • 35
  • 56

1 Answers1

62

In case you aren't using ajax, or are still on JSF 1.x, and you really need to invoke a business action in cancel() method (i.e. just reloading the page is insufficient), then your best bet is to add immediate="true" to the button. This way all input fields which don't have immediate="true" will be skipped in processing.

<h:commandButton value="Cancel" action="#{bean.cancel}" immediate="true" />

On JSF 2.x, much better is to submit the form by <f:ajax>, which by default only processes @this instead of @form.

<h:commandButton value="Cancel" action="#{bean.cancel}">
    <f:ajax />
</h:commandButton>

If you want to navigate to another page here, add ?faces-redirect=true to the outcome in the cancel() method.

Or, if you actually don't need to invoke any business action at all, then just use <h:button> wherein you directly specify the (implicit) navigation case outcome.

<h:button value="Cancel" outcome="previouspage" />

This will basically reload the page by a GET request. The <h:button> doesn't exist in JSF 1.x, but you can also just use plain HTML+JS for that.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Follow-up: How can it be done if I have e.g. ` – sl3dg3 Jun 04 '14 at 08:26
  • 1
    @BalusC Do you know a way to skip the validation phase but process the update model values phase? Is this possible in JSF 2? – FuryFart Jun 05 '15 at 12:48
  • @SurprisedCoconut Yes,this can be possible in JSF 2 by removing validators Only for the PROCESS_VALIDATIONS phase in your PhaseListener Class – User0123 Sep 21 '16 at 08:37