Skip to end of metadata
Go to start of metadata

Some knowledge of wicket property-resource loading is required to understand this article.

As of wicket-1.1 post rc2 there is an easier way to provide validation messages for form components.

In wicket-1.1-rc2 and before the validation messages were provided through resource keys defined as form-id.form-component-id.validator-class-name. This approach entailed a lot of repetitive resource messages. For example, the following form:

would have required the following resource keys:

Wicket-1.1 post rc2 provides a much simplified approach by introducing the ${label} variable in the validation context which represents the user-centric name of the form component and allows factoring out validator-specific messages into one place.
_IMPORTANT: This only works correctly, when getResourceSettings().getUseDefaultOnMissingResource is true !

The ${label} variable can be provided by one of two ways:
It can either be set directly on the form component instance by calling setLabel(IModel model) method, or by including a resource key of the form form-id.relative_path_to_form-component-id.

IMPORTANT: If your form component is a child of another component (e.g. Border) and isn't being directly added to the form component itself, then you need to use the relative path to the component.  

The example above can now be rewritten as following:

Specify the general validator messages:

Specify the user-centric form component labels:


or specify labels directly in java code:

This approach makes it much simpler to maintain error messages in forms.

Since 1.2 the following features have been added:
Let's say you have the following component hierarchy:

XPanel knows nothing about its containing form or page, nevertheless XPanel's validation keys was required to be prefixed with the form's id

That introduced an unwanted dependency of XPanel on its parental form (i.e. id="b").

Furthermore there was no way to override the foo messages separately for XPanel and YPanel:

That has been changed. The search order has been changed like that:

YPanel's validation text could stay unaltered:

Changes in Wicket 1.3

Wether a field is required is now determined by testing the isRequired() method of FormComponent, therefore 'RequiredValidator' in the above examples has to be replaced by 'Required'.

  • No labels