How to skip validation when a specific button is clicked?

后端 未结 1 1269
囚心锁ツ
囚心锁ツ 2020-11-28 06:41

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

1条回答
  •  失恋的感觉
    2020-11-28 07:31

    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.

    
    

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

    
        
    
    

    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 wherein you directly specify the (implicit) navigation case outcome.

    
    

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

    See also:

    • Why was "immediate" attribute added to the EditableValueHolders?
    • How to navigate in JSF? How to make URL reflect current page (and not previous one)
    • How to let validation depend on the pressed button?

    0 讨论(0)
提交回复
热议问题