You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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.

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.

Example:

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

public CustomForm(String form)

Unknown macro: { 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()

Unknown macro: { //what to do when this "Ok" button is click log.info("Ok button pressed"); }

//This method is in CancelButton class
public void onSubmit()

Unknown macro: { //Do the "Cancel" thing when click log.info("Cancel button pressed"); }

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()

Unknown macro: { info("OK was pressed!"); }

});
Button cancel = new Button("cancelbutton"){
protected void onSubmit()

Unknown macro: { info("Cancel was pressed!"); }

};
cancel.setDefaultFormProcessing(false);
add(cancel);
}
}

with the HTML being

<form ...>
<input type="submit" wicket:id="okbutton value="OK" />
<input type="submit" wicket:id="cancelbutton value="Cancel" />
</form>

  • No labels