Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Excerpt
hiddentrue

How to use multiple submit buttons in forms


When we have a form with only one submit button, the default Form's onSubmit() method will be called. If the Form has more than one submit button, then the mechanism works differently.

...

We have a simple Form that contains two submit buttons. The class contructor looks something like this:

unmigrated-wiki-markup
Panel
Code Block

    public CustomForm(String form) {
        add(new OkayButton());
        Button cancel = new CancelButton();
        cancel.setDefaultFormProcessing(false);
        add(cancel);
    }

Note: without setDefaultFormProcessing(false), the "cancel" button is subject to validation like the "Ok" button.

The we have two custom Button classes with the override onSubmit() that looks something like below:


    //This method is in OkayButton class
    public void onSubmit() {
      //what to do when this "Ok" button is click
      log.info("Ok button pressed");
    }

    //This method is in CancelButton class
    public void onSubmit() {
      //Do the "Cancel" thing when click
      log.info("Cancel button pressed");
    }
Code Block
Panel
Wiki Markup
Panel
Wiki Markup

Alternatively, this can also be done with anonymous inner classes, e.g.


    private static class MyForm extends Form {
        public MyForm(String name) {
            super(name);
            add(new Button("okbutton"){
                protected void onSubmit() {
                    info("OK was pressed!");
                }
            });
            Button cancel = new Button("cancelbutton"){
                protected void onSubmit() {
                    info("Cancel was pressed!");
                }
            };
            cancel.setDefaultFormProcessing(false);
            add(cancel);
        }
    }
Code Block
Panel
Wiki Markup

with the HTML being

Code Block
html
html

  <form 
Panel
<form
...>

<input

      <input type="submit" wicket:id="okbutton value="OK" />

<input

      <input type="submit" wicket:id="cancelbutton value="Cancel" />


  </form>