Make localized field names lookup extensible in JSR-303 validation messages [SPR-14104] #18676
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
Bertrand Guay-Paquet opened SPR-14104 and commented
When using JSR-303 annotations for validation, Spring looks up error messages in message bundles and makes available the field name as well as the annotation attribute values in alphabetical order. These arguments are determined by LocalValidatorFactoryBean via its superclass SpringValidatorAdapter with this cose:
Currently, for a field named "fooField" of the class "barClass", the message keys would be ["barClass.fooField", "fooField"]. I believe this was implemented in #11073.
This can then be used in a message.properties file (for a
@Size
annotation) like so:where {0} is either the localized field name (using the 2 keys described above) or the field name itself as a fallback.
This works great, but does not allow for using "namespaced" common field names in messages.properties like so:
I coded this custom validator factory bean which achieves this:
Now, for the feature request: I think it would be useful to externalize the choice of field name message codes to a protected method which could be overwritten by child classes. In this way, the implementation would be much cleaner and more robust.
To be clear, I'm proposing to change this line in SpringValidatorAdapter :
to something like :
Affects: 4.2.5
Referenced from: commits 696dcb7
The text was updated successfully, but these errors were encountered: