Customize validation message in Wicket

Asked

Viewed 23 times

0

I currently have a project that is using Wicket 7.8.0 and Java 8 and I have one DateTextField that every time I put one wrong date, the FeedbackPanel reports that the: "deDateField" não é um número válido.

I’d like to customize this message from DataTextField for "Desde(de) não é uma data válida".

I’m currently starting the DateTextField as follows:

deDateField = new DateTextField("deDateField", new PropertyModel<Date>(this, "deDate"), DateUtils.PATTERN);

1 answer

1


When we talk about validation messages, whether in Wicket or Hibernate for example, we always have to keep in mind the internationalization of messages.

To apply internationalization to messages, usually validators indicate which property name should be searched for from the properties related to the desired region, in the case of Wicket, messages can be located in the Application_xx.properties, that is inside the package wicket-core.

In your case, the framework is displaying the message in Portuguese, from of these properties.

IConverter.Date='${label}' n\u00E3o \u00E9 um n\u00FAmero v\u00E1lido.
IConverter.date='${label}' n\u00E3o \u00E9 um n\u00FAmero v\u00E1lido.

There are two ways to customize this message:

1) Creating properties for the page/class you are working on

Dentro de um arquivo nomeado MinhaPagina.properties:

meuForm.meuCampoDeData.IConverter.Date='${label}' n\u00E3o \u00E9 uma data v\u00E1lida
meuForm.meuCampoDeData.IConverter.date='${label}' n\u00E3o \u00E9 uma data v\u00E1lida

Note that in this case you must respect the form hierarchy.

2) Creating a global properties for your application

Dentro de um arquivo nomeado Application.properties:

IConverter.Date='${label}' n\u00E3o \u00E9 uma data v\u00E1lida
IConverter.date='${label}' n\u00E3o \u00E9 uma data v\u00E1lida

The settings will always be rewritten by the most specific ones, for example, your application’s properties settings (case 2) will overwrite the framework’s default messages, as well as the settings per page (case 1) will overwrite those of your application (case 2).

More complete models can be found on documentation


One thing I forgot to mention is that ${label} will be replaced by label from your field, so you will also need to adjust it so that the message is in line with your expectations.

  • 1

    Thank you very much. It was easier than I thought, for my case it was enough to respect the hierarchy of the form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.