Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve default validation messages from Hibernate Validator?

I'm trying to retrieve a default validation error-message using MessageSource. The code I'm working with uses reflection to retrieve the value of the message parameter. On a constraint that does not override the message parameter, I would like to retrieve the default error message. When I invoke the message method on the validation annotation, I get {org.hibernate.validator.constraints.NotBlank.message} (for example, for the @NotBlank annotation). I then tried to use MessageSource to get the error message like so:

String message = messageSource.getMessage(key, null, Locale.US);

I tried setting key to {org.hibernate.validator.constraints.NotBlank.message}, org.hibernate.validator.constraints.NotBlank.message (removed braces) and even org.hibernate.validator.constraints.NotBlank but I keep getting null. What am I doing wrong here?

UPDATE

A clarification. I am under the impression that Spring comes with a default message.properties file for its constraints. Am I correct in this assumption?

UPDATE

Changing the name of the question to better reflect what I was trying to do.

like image 980
Vivin Paliath Avatar asked Jul 30 '10 14:07

Vivin Paliath


People also ask

What is the difference between javax validation and Hibernate Validator?

The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.

How does Hibernate Validator work?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.

Which is the default Validator of Spring?

Spring MVC Framework supports JSR-303 specs by default and all we need is to add JSR-303 and it's implementation dependencies in Spring MVC application.

Is Hibernate Validator deprecated?

Note that since Hibernate 5, Hibernate deprecated several of its specific validation constraint classes in favor to standard JSR 380-bean validation classes. Note also that the Hibernate Validator 6 library is actually the reference implementation of this JSR.


1 Answers

After running into a blog post from one of the Hibernate guys, and after digging around in the Hibernate Validator source, I think I've figured it out:

public String getMessage(final Locale locale, final String key) {
    PlatformResourceBundleLocator bundleLocator = new PlatformResourceBundleLocator("org.hibernate.validator.ValidationMessages");
    ResourceBundle resourceBundle = bundleLocator.getResourceBundle(locale);

    try {
       final String k = key.replace("{", "").replace("}", "");
       return resourceBundle.getString(k);
    }
    catch (MissingResourceException e) {
       return key;
    }
}

So first, you have to instantiate a PlatformResourceBundleLocator with the default validation messages. Then you retrieve a ResourceBundle from the locator and use that to get your message. I don't believe this performs any interpolation though. For that you have to use an interpolator; the blog post I linked to above goes into more detail about that.

UPDATE

Another (easier) way is to update your applicationContext.xml and do this:

<bean id="resourceBundleSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>org.hibernate.validator.ValidationMessages</value>
        </list>
    </property>
</bean>

Now your MessageSource is populated with the default messages and you can do messageSource.getMessage(). In fact, this is probably the best way.

like image 141
Vivin Paliath Avatar answered Oct 24 '22 14:10

Vivin Paliath