Implementing Cancel button for Wicket form and enforcing navigation

258 Views Asked by At

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

2

There are 2 best solutions below

0
martin-g On BEST ANSWER

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.
0
AudioBubble On

You can disable form validation with the option setDefaultFormProcessing:

Button cancelButton = new Button("cancel_button") {
    @Override
    public void onSubmit() {
        setResponsePage(ViewEntityPage.class);
    }
};
cancelButton.setDefaultFormProcessing(false);

see also the official JavaDoc of that Method:

https://nightlies.apache.org/wicket/apidocs/9.x/org/apache/wicket/markup/html/form/Button.html#setDefaultFormProcessing(boolean)