Reputation: 2513
I use Spring Boot and Hibernate validator and Thymeleaf.
Everything is ok when I validate a form item.
But after I add a verify code, I check if it's wrong code;
When the code is wrong I add a new FieldError
and return it.
if (!captchaCode.equalsIgnoreCase(user.getVrifyCode())) {
bindingResult.addError(new FieldError("user", "vrifyCode", "{singup.vrifycode.error}"));
}
And of course I defined the singup.vrifycode.error
in my ValidationMessages.properties file.
But at web page it just show singup.vrifycode.error
not the correct value.
But in my model everything is ok like this:
@NotBlank(message="{signup.email.null}")
@Email(message="{signup.email.fomart}")
private String email;
How can I show correct value in my page?
If I can not, tell me how can I get the correct value(by locale) in ValidationMessages_en_US.properties, ValidationMessages_ja_JP.properties and so on.
Thanks!
Upvotes: 2
Views: 1340
Reputation: 16604
First we must understand that there are two different validations API:s in the mix here; first the Spring validation with BindingResult
and FieldError
classes, and then the Bean Validation API (Hibernate Validator) with the annotations. The good news is that Spring translates validation errors from Bean Validation API, so these work together.
One difference is that Spring doesn't use {
and }
around its translation keys (in spring called codes), keep that in mind.
For i18n you must use the 7-arguments constructor of FieldError:
new FieldError("user", "vrifyCode", user.getVrifyCode(), false, new String[] {"singup.vrifycode.error"}, null, null)
Note that the translation should be in the Spring messages file, because it is Spring validation we are working with here.
Upvotes: 3