Not able to override @NotEmpty default message using spring message source

Description

I'm using hibernate-validator in a Spring MVC based application. The validation annotations like @NotEmpty are used in the entity objects. I want to override the default message used for the @NotEmpty annotation.

This is working and the message key is resolved by the spring message source:

@NotEmpty(message="myMessageKey")

My idea was to use plain @NotEmpty annotation (without specifying a message key) and to override/set the default message key in my spring message source. I assume the default message key is org.hibernate.validator.constraints.NotEmpty.message and I expected that the text gets resolved by my spring message source the same way as myMessageKey. But unfortunatley this does not work. org.hibernate.validator.constraints.NotEmpty.message key is resolved by Hibernate Validator message bundles.

Environment

None

Activity

Show:

Christian PlätzingerMay 28, 2015 at 6:28 AM
Edited

Thanks a lot for your detailed reply. Your hint regarding the usage of curly braces for the message keys was a good one. I missed that before. So putting the hibernate validation message keys including the curly braces into our spring message source works. We are now able to use plain @NotEmpty etc. annotations with customized messages. From my point of view this ticket can be closed.

Hardy FerentschikMay 27, 2015 at 8:46 AM

I think there are two things at play, Hibernate Validator and its message interpolation/resolution via ValidationMessages.properties as specified by the Bean Validation spec and the interpolation of Spring MVC using messages.properties.

The Spring integration (most likely, I don't know the code) calls first the Validator validation routine and then resolves the resulting error messages (if any) against its messages.properties.

Note also, that Bean Validation uses the curly braces ('{', '}') for message keys and variables. So the default message for @NotEmpty is really {org.hibernate.validator.constraints.NotEmpty.message}. Using this default, the resolution will occur within the Validator bundles. It works for myMessageKey (or in fact probably also for org.hibernate.validator.constraints.NotEmpty.message), since in this case Validator won't do any interpolation and the message is passed as is to Spring MVC message interpolation.

To make your use case work, the message interpolation as part of Bean Validation needs to be prevented. There is already a possible solution for this. Bean Validation defines the MessageInterpolator interface to plug in custom interpolators. You could just provide (at bootstrap) a no-op interpolator which always returns the non interpolated string. Of course the key in messages.properties would need to be {org.hibernate.validator.constraints.NotEmpty.message}.

The question is whether it would make sense for Validator to provide such a no-op message interpolator or not? One could also imagine to provide a configuration property org.hibernate.validator.skip_message_interpolation to make configuration potentially a bit easier.

I am atm a bit undecided whether this is worth doing from a Validator specific perspective or whether it is really up to the user to provide such a message interpolator. Also the for Spring integration code could be changed to allow this option.

Rejected

Details

Assignee

Reporter

Bug Testcase Reminder (view)

Bug reports should generally be accompanied by a test case!

Bug Testcase Reminder (edit)

Bug reports should generally be accompanied by a test case!

Participants

Christian Plätzinger
Hardy Ferentschik

Affects versions

Priority

Created May 27, 2015 at 6:22 AM
Updated May 28, 2015 at 8:52 AM
Resolved May 28, 2015 at 8:52 AM

Flag notifications