Chapter 6: Form Rituals - Purify user input

Form Basics

In Wicket, a Form wraps related inputs and centralizes submit behavior. Define intent clearly in onSubmit() and handle failure in onError().

Form Components

Use TextField, PasswordTextField, TextArea, CheckBox, and DropDownChoice with typed models for predictable binding.

Validation

Combine built-in validators with domain-specific rules. Reject invalid input early and return explicit, user-readable messages.

Validation example
nameField.setRequired(true);
nameField.add(StringValidator.lengthBetween(3, 100));

FeedbackPanel

FeedbackPanel is the canonical place to surface success and error messages from form processing.

Lifecycle

  1. Receive request parameters.
  2. Convert input to target types.
  3. Validate values.
  4. Update models.
  5. Execute submit handlers.

Practical Pitfalls

  • Problem
    TextField values disappear after AJAX submit, even though users just entered them.
    Cause
    Input components are not properly bound to models, so rerender has no stable value source.
    Solution
    Create all form fields with explicit models and verify values survive rerender paths.
  • Problem
    DropDownChoice cannot return to an unselected state.
    Cause
    Null selection is not allowed by current component configuration.
    Solution
    If requirements allow blank state, enable null selection and align validation accordingly.
  • Problem
    Form output changes depending on interaction order.
    Cause
    Multiple inputs share one model and final state depends on update sequence.
    Solution
    Split models by input responsibility, or enforce explicit update order with tests.
  • Problem
    Submit and validation behavior becomes unstable after UI restructuring.
    Cause
    Nested forms violate HTML rules and browser interpretation becomes unreliable.
    Solution
    Keep form boundaries flat and use Panel/Fragment only for visual grouping.