How do I define my own feedback messages in Wicket? For example: if I give an incorrect username, I want to get an error message like "The user name in incorrect, try to login again." instead of using the default error message.
What would an example be like?
you should set Feed back message to Session
You can display your own error messages using
error()
andwarn()
andinfo()
. If you want to show errors dependent on validators or the required flag you can define a properties file with the same name as the class which contains a mapping of field -> message. For example:Index.java
Index.properties
All required fields
The field
name
in the formmyform
when it is required.Any field with the name
password
when it is required.Any field with the name
phonenumber
when it is required.This shows a variety of ways of setting a feedback message for specific components.
You can also put the properties files next to the following component level (in order of importance, top highest):
Hope that helps
@user1090145: I've used overloaded Component's error() method in Validator's class:
and invoked it in validate() by
Properties your-form.field.text-id must be defined in YourPage.properties
Sources: Create custom validator in Wicket and Form validation messages
You can use an anonymous IValidationError class and the messageSource.getMessage method to get a custom message from your property file:
Sample property file: