views:

302

answers:

2

We're using spring with annotations, and our annotated fields get validated fine and error messages get displayed from ValidationMessages.properties, but for custom validation message from ValidationMessages.properties does not seem to be used.

Here's the example:

Validatior (also the form):

public void validateSpecial(BindingResult result) {

    if(password != null && !password.equals(passwordConfirm)){
       result.rejectValue("passwordConfirm", "emailform.passwordConfirm.passwordsDontMatch", new Object[]{}, "This message is in the code."); 

... }

ValidationMessages.properties:

...
emailform.passwordConfirm.passwordsDontMatch = Passwords don't match
...

Controller:

...
    form.validateSpecial(result);
    if (result.hasErrors()) {
        return "/path/to/input/page";
    }
...

The error that I get is "This message is in the code", not "Passwords don't match"

A: 

Do you have ResourceBundleMessageSource defined in your application context? It is needed to pick up resource bundles. This might be a good link http://wheelersoftware.com/articles/spring-bean-validation-framework.html

Kartik
How should ResourceBundleMessageSource be defined in applicationContext? is there a specific bean id that I need to use? Messages for annotated fields get resolved no problem.
Mike
A: 

This solved the problem, thanks @Kartik

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>ValidationMessages</value>
            <value>ApplicationResources</value>            
        </list>
    </property>
</bean>
Mike
So it seems like when it comes to annotated errors, Spring 3.0.X finds the messageSource w/o it explicitly defined in applicationContext, but when it comes to custom validator, spring needs messageSource defined in applicationContext
Mike
Sorry for not answering earlier. I just checked a stackoverflow until now. I am glad I was able to help in some way.
Kartik