-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide localized field name in JSR-303 validation messages [SPR-6407] #11073
Comments
Pavla Nováková commented Hi Sebastian, good idea, I'm using little bit different approach to implement required behaviour, it may be helpful: Logic implemented in my custom BeanValidator - see below (that integrates JSR-303 Validator and Spring MVC Validator) may be moved framework and the second part of this solution is to use custom format for JSR-303 ValidationMessages.properties : property value is using delimiter "-" for message key to spring message source and its arguments. Example format of ValidationMessages.properties:
Then in my simple Spring message source - messages.properties
Finally the implementation of my BeanValidator is:
This way I can use flexible Spring MVC error message resolution integrated with JSR-303 Validation. |
Juergen Hoeller commented Sebastian, I've implemented your suggestion for Spring 3.0 RC3 now since it really is very consistent with the established processing of bind errors in Spring. Pavla, there are certainly alternative approaches here: We'll revisit this topic for Spring 3.1, based on the overall feedback that we'll be getting in the meantime. Juergen |
Sebastian Beigel opened SPR-6407 and commented
I think it would be a good idea to provide the (localized) field name in JSR-303 validation messages as the first argument ("{0}"). It would be consistent with Spring's other validation (type conversion) messages and essential if you want to show a validation error summary (vs. showing the error message next to the input field in the UI, i.e. "Foo must match [a-z]+").
I have extended LocalValidatorFactoryBean and overridden validate(Object target, Errors errors) to include the field name (as a DefaultMessageSourceResolvable) as the first argument. But I think it should be included directly in LocalValidatorFactoryBean (i.e. SpringValidatorAdapter).
The code could look something like this (sorry, too lazy to make a patch :)
Resolving to messages like this:
Pattern={0} must match {4}!
model.field=Foo
field=Bar
=> Foo must match [a-z]+
Of course you could provide all the messages in the "direct" form: "Pattern.field=Field must match {4}" (w/out the patch) -- but this is cumbersome and redundant :)
Affects: 3.0 RC2
Referenced from: commits 69124f9
The text was updated successfully, but these errors were encountered: