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
- Receive request parameters.
- Convert input to target types.
- Validate values.
- Update models.
- Execute submit handlers.
Practical Pitfalls
-
ProblemTextField values disappear after AJAX submit, even though users just entered them.CauseInput components are not properly bound to models, so rerender has no stable value source.SolutionCreate all form fields with explicit models and verify values survive rerender paths.
-
ProblemDropDownChoice cannot return to an unselected state.CauseNull selection is not allowed by current component configuration.SolutionIf requirements allow blank state, enable null selection and align validation accordingly.
-
ProblemForm output changes depending on interaction order.CauseMultiple inputs share one model and final state depends on update sequence.SolutionSplit models by input responsibility, or enforce explicit update order with tests.
-
ProblemSubmit and validation behavior becomes unstable after UI restructuring.CauseNested forms violate HTML rules and browser interpretation becomes unreliable.SolutionKeep form boundaries flat and use Panel/Fragment only for visual grouping.