Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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.

Info

It might be a good idea to use input instead of button elements in your form hierarchy. IE tends to send all button values, making it difficult to establish the one used to submit.

To have more than one submit buttons in a Form, we need to create custom Button by subclassing from wicket.markup.html.form.Button. We define the the custom Button behaviour by overiding the onSubmit() method and add the buttons to the Form. When the user click on any of the buttons, the specific behaviour of the button is executed by calling the button's onSubmit() method followed by execution of form's onSubmit() method.

Example:

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

...

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

...

Alternatively, you could use the form's onSubmit() method for the submit triggered by the OK button, and the Cancel button's onSubmit() method for the submit triggered by the Cancel button.

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

    protected void onSubmit() {
        info("OK was pressed!");
    }
}