Search code examples
javawicket

Implementing Cancel button for Wicket form and enforcing navigation


I have a form to add an entity to the database. This form has a Save and Cancel button and also two input fields which are marked with required.

While the save functionality works fine, the cancel functionality does not work as expected: clicking cancel should bring the user back to another page but it should not validate the form.

Form<AddEntityPage> form = new Form<>("form_1", new CompoundPropertyModel<>(this));
form.add(new RequiredTextField<>("name"));
...
form.add(new Button("cancel_button") {
            @Override
            public void onSubmit() {
                setResponsePage(ViewEntityPage.class);
            }
        });
<form wicket:id="form_1">
  <table>
    ...
    <tr>
      <td>
        <button name="cancel"
                value="Cancel"
                wicket:id="cancel_button">Cancel</button>
      </td>
      ...
    </tr>
  </table>
</form>

So what is the ideal way for aborting form processing? I simple to enforce the navigation to the response page.

enter image description here


Solution

  • You need to do cancelButton.setDefaultFormProcessing(false).

    As its javadoc says:

     * Sets the defaultFormProcessing property. When false (default is true), all validation and
     * form updating is bypassed and the onSubmit method of that button is called directly, and the
     * onSubmit method of the parent form is not called. A common use for this is to create a cancel
     * button.