I am trying to configure Hibernate Validator to use messages from MessageSource. I have the following setting in messages-context.xml:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>WEB-INF/messages/messages</value>
<value>WEB-INF/messages/exceptions</value>
<value>WEB-INF/messages/fields</value>
<value>WEB-INF/messages/buttons</value>
<value>WEB-INF/messages/validation_errors</value>
</list>
</property>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en_GB" />
</bean>
I tried various approaches to how to pass the message key to the hibernate validator (both with closing and without {}, as well as without specifying a user key - simply using the default value:
@NotEmpty(message="validation.required.field")
@Length(max=255, message="{validation.too.long}")
private String firstName;
@NotEmpty(message="{validation.required.field}")
@Length(max=255, message="{validation.too.long}")
private String lastName;
@NotNull
@Past(message="{validation.must.be.past}")
private Date dateOfBirth;
@NotEmpty(message="{validation.required.field}")
@Length(max=255, message="{validation.too.long}")
private String email;
My validation_errors_en_GB.propertieslooks like this:
validation.required.field=this is a required field
validation.too.long=this field can only take {0} characters
validation.must.be.past=this date has to be in the past
javax.validation.constraints.NotNull.message=Custom message
However, when blank fields are checked, the following messages are displayed:
First name validation.required.field
Last name {validation.required.field}
Date of birth may not be null
Email {validation.required.field}
For some reason, the message key is always used - the actual message is never viewed. Any idea where I'm wrong?
Thank,
Russell