Versions Compared

Key

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

...

In some cases, however, whether or not the field is required cannot be determined when the form is created. Like many properties in Wicket, we can override the property setter getter (isRequired, in this case) to defer the evaluation of the property until the last possible moment:

...

Code Block
Button submit = new Button("submit") {
    public void onSubmit() {
        // handle form submission
    }
}
form.add(submit);

TextField foo = new TextField("foo") {
    public boolean isRequired() {
        Form form = (Form) findParent(Form.class);
        return form.getRootForm().findSubmittingButton() == submitButtonbutton;
    }
}
form.add(foo);

Note the call to getRootForm. Technically, this is only required in nested forms.

...

Code Block
new Button("submit").setDefaultFormProcessing(false);

Validating just the button-press

Simply add a validator to the button which checks if it was pressed:

Code Block

button.add(new IValidator<String>() {
        @Override
        public void validate(IValidatable<String> validatable) {
          if (button.getForm().findSubmittingButton() == button) {
            // ... do your test here
            if (errorFound) {
              ValidationError error = new ValidationError().addMessageKey("errorFound_button");
              error.setVariables(Collections.singletonMap("parameter", (Object) "value"));
              validatable.error(error);
            } 
          }
        }
      });

Alternative Approach

Another approach to enabling validation based on which submit button was used is to take over the form processing workflow as follows.

Say we have four form components within the same form:

  1. name A
  2. description A
  3. name B
  4. description B

Sometimes we want the whole form to validate/process (Button C), but sometimes we only want "A" form components to validate/process (Button A). One way to accomplish this is to:

Code Block

final TextField nameA = new TextField("name", ...);
final TextField descriptionA = new TextField("descriptionA", ...);
final TextField nameB = new TextField("nameB", ...);
final TextField descriptionB = new TextField("descriptionB", ...);

// the validators can be anything, but for simplicity we just use required
nameA.setRequired(true);
descriptionA.setRequired(true);
nameB.setRequired(true);
descriptionB.setRequired(true);

final Button buttonA = new Button(id) {
    public boolean onSubmit() {
        // because we overrode the form processing we need to handle validation/processing on the components ourselves
        nameA.validate();
        descriptionA.validate();
        if(!nameA.isValid() || !descriptionA.isValid()){
                // didn't validate so we stop processing (validation errors will be displayed provided we are using a FeedbackPanel or similar)
                return;
        }
        // TODO : now we have the updated values/models so we can perform whatever button A is supposed to do
    }
});
// set the form processing to false so that no validation/processing will occur on the form when button A is clicked
buttonA.setDefaultFormProcessing(false);

// we can do the same thing for B components
final Button buttonB = new Button(id) {
    public boolean onSubmit() {
        // because we overrode the form processing we need to handle validation/processing on the components ourselves
        nameB.validate();
        descriptionB.validate();
        if(!nameB.isValid() || !descriptionB.isValid()){
                // didn't validate so we stop processing (validation errors will be displayed provided we are using a FeedbackPanel or similar)
                return;
        }
        // TODO : now we have the updated values/models so we can perform whatever button B is supposed to do
    }
});
// set the form processing to false so that no validation/processing will occur on the form when button B is clicked
buttonB.setDefaultFormProcessing(false);

final Button buttonC = new Button(id) {
    public boolean onSubmit() {
        // TODO : all validation passed because form processing is true so we can perform whatever button C is supposed to do
    }
});

CheckBox

In this recipe, the field is only required when a checkbox on the form is checked:

...

Code Block
public class DocumentType {
    private String name;
    private boolean hasExpiryDate;
    // getters and setters omitted
}

List<DocumentType> allDocumentTypes = ...;

final DropDownChoice ddc = new DropDownChoice("documentType", allDocumentTypes, new ChoiceRenderer("name"));
form.add(ddc);

TextField expiryDate = new TextField("expiryDate", Date.class) {
    public boolean isRequired() {
        DocumentType dt = (DocumentType) ddc.getConvertedInput();
        return dt != null && dt.getHasExpiryDate();
    }
}
form.add(expiryDate);

Isolating Nested Forms

Consider a nested form with its own submit button. When this button is clicked, only components on the nested form are validated. However, when a button on the parent form is clicked, components on both the parent form and the child form are validated.

Occasionally, you may want the forms to be truly independent, i.e. you want the parent form to not validate and submit the child form.

Code Block

class InternalForm<T> extends Form<T> implements IFormVisitorParticipant {
    public InternalForm(String name) {
        super(name);
    }

    public InternalForm(String name, IModel<T> model) {
        super(name, model);
    }

    public boolean processChildren() {
        IFormSubmittingComponent submitter = getRootForm().findSubmittingButton();
        if (submitter == null)
            return false;
        
        return submitter.getForm() == this;
    }
}

Form nestedForm = new InternalForm("nestedForm");