1

I have a project using spring 3 mvc and hibernate validator to validate form object.

I follow spring's document

In my controller, I can get bindingresult, which bindingresult.hasErrors() == true.

I use ReloadableResourceBundleMessageSource to do the error message binding.
And my question is: Is it possible that I can have a more flexible error message display in my jsp web page?

For example, I want to say "the app name UserEnteredAppName is not valid. The size should be between 2 and 255".

In my message.property, I define:

Size.appNameForm.itemName=the app name {What Should I do in this brace?} is not valid. The size should be between {1} and {2}

{1} and {2} can be interpolated to '2' and '255'. but how can I get the UserEnteredAppName in the interpolated message?

Below is my form class and controller class:

My controller class:

@RequestMapping(value="foo.htm")
public String handleAppNameFormSubmit(@Valid @ModelAttribute("appNameForm") AppNameForm form, BindingResult result){
}

My form class:

public class AppNameForm implements IForm{
    @NotEmpty
    @Size(min = 1, max = 255)
    private String itemName;
    ....
}

I find some links which give me some hint:

for example: How do I dynamically resolve message parameters with Hibernate Validator?

But I am using spring mvc and spring controll which jsr303 implementation to use, I cant find a way to use custom validator, extending hibernate's default ValidatorImpl.

What I am thinking the solution is, I can have custom validator, which will reject the UserEnteredAppName into the bindingResult. is it possible? or I should find another way?

Thanks Andrew

1
  • FYI, in my jsp, I use spring tags to bind the error message: <form:error path="itemName"/> Commented Jul 31, 2011 at 22:09

1 Answer 1

1

As of release 4.2 Hibernate Validator provides ValueFormatterMessageInterpolator, a new message interpolator which should fulfill your requirements.

With that interpolator you can refer to the validated value using the place holder ${validatedValue}. You can also specify a format string to be applied like this: ${validatedValue:[format string]}, e.g. ${validatedValue:%1$ty} if the validated value is a date.

An example for using this interpolator can be found in the Hibernate Validator reference guide.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using Spring 3 MVC. So the error message is handle by ReloadableResourceBundleMessageSource(spring class). Is it possible that I can use hibernate validator's message interpolator ?
You should be able to use ValueFormatterMessageInterpolator in conjunction with Spring's MessageSourceResourceBundleLocatorby setting the following interpolator at Spring's LocalValidatorFactoryBean: new ValueFormatterMessageInterpolator(new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(yourReloadableMessageSource)))
An addition: Given that you are working with a reloadable message source you probably should use the constructor ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator, boolean cacheMessages) which allows to disable the internal caching of resolved messages in Hibernate Validator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.