Skip to content

Access JSR-303 validation contraint attributes in localized messages [SPR-6730] #11396

@spring-projects-issues

Description

@spring-projects-issues

Scott Frederick opened SPR-6730 and commented

Related to: #11073 and #10092.

When using declarative validation constraints with Spring, custom/localized messages should be able to access the arguments to the validation annotation in a consistent manner that is guaranteed to work across validator providers.

For example, when applying an annotation like "@Size(min=1, max=25)" to a field you should be able to provide an error message format like "Size.fieldName=Size of {0} must be between {1} and {2}".

Currently, the values of the arguments passed to the annotation are available to the message source in an arguments array, but the position of each value in the array is not consistent. It appears that the positions of the arguments in the array can change depending on the validator provider and potentially change between releases of a validator provider. If the framework does currently provide some guarantee of the order, then this should be documented.

Here are some examples of validator annotations and the arguments array that is available to the message source when the validation fails. In all cases the first ([0]) element in the array is a DefaultMessageSourceResolvable containing the name of the field in error.

@Size(min=1, max=25)

index contents
1 "message" argument from annotation
2 "min" argument from annotation
3 "payload" argument from annotation
4 "max" attribute of annotation
5 "groups" attribute of annotation

@Max(25)

index contents
1 "value" argument from annotation
2 "message" argument from annotation
3 "groups" attribute of annotation
4 "payload" argument from annotation

@Min(1)

index contents
1 "value" argument from annotation
2 "message" argument from annotation
3 "groups" attribute of annotation
4 "payload" argument from annotation

@Pattern(regexp="[A-Z]+")

index contents
1 "message" argument from annotation
2 "payload" argument from annotation
3 "flags" argument from annotation
4 "regexp" attribute of annotation
5 "groups" attribute of annotation

Affects: 3.0 GA

Reference URL: http://forum.springsource.org/showthread.php?p=279199#post279199

Attachments:

Issue Links:

Referenced from: commits 7304c09

3 votes, 9 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions