Search code examples
wicketwicket-6

wicket 6 custom error message instead of The value of x is not a valid int


I am using Wicket 6, I want to add custom error message for a specific component (TextField).

This field should only accept number within specific value (0-59). This validation works perfectly. However, when the user enters a non-number... it seems to return a default validation error message. Something like The value of x is not a valid int.

Below is the sample code on how I set the validation/error message when the entered value is greater than 59 or less than zero:

    ...

    form.add(new ReportErrorControlGroup("minutesLateGroup", new TextField("minutesLate",
            new PropertyModel<String>(m_attendanceInfo, "minutesLate")).setRequired(false).add(new IValidator<Integer>() {
        private static final long serialVersionUID = 1L;

        @Override
        public void validate(IValidatable<Integer> validatable) {
            Integer input = validatable.getValue();
            if (input < 0 || input >= 60) {
                validatable.error(new ValidationError("Please enter a valid minute(s) value, a value between 0 to 59."));
            }
        }
    })));

    ...

When the user entered a non-number value, it will return a validation/error message The value of 'minutesLate' is not a valid int.

What I want is to replace this error with something like Please enter valid minute value. and to be applied only to minutesLate.

In my other project, I can set this via messages.properties then something like field.error.incorrect.expected.values={0} is not a valid value.

But this project is Apache Wicket 6 and I have no idea on how to setup/configure messages.properties and use specific message to be displayed when validation fails.

I am thinking of using minutesLate.value.invalid=Please enter valid minute value.

How can I make it see the added messages.properties and use minutesLate.value.invalid for invalid (non-number) values.


Solution

  • The default messages are defined at https://github.com/apache/wicket/blob/c6fde82ce9637c43753b59620b2f5551ad7f20fe/wicket-core/src/main/java/org/apache/wicket/Application.properties#L16

    So, you need to override it in MyApplication.properties (must be next to MyApplication.class)

    minutesLate.IConverter=Please enter valid minute value.