First, let me explain that I'm using Spring MVC 3.1.1, and Hibernate validation 4.2.0. I'm using validation annotations for various forms in my Spring application. Since my application needs to be localized, I've been using a resource bundle for my validation messages that looks somewhat like this:
# ValidationMessages.bundle
FieldMatch=Password and confirmation must match.
The corresponding class definition for this message looks like this:
@FieldMatch.List({
@FieldMatch(first = "password", second = "passwordConfirmation")
})
public class RegistrationForm {
// ...
}
I have this custom resource bundle set up in my appContext.xml, and my messages are being shown on the form without any issue.
Here's my dilemma, however. There is a new requirement that I must confirm more fields match. Right now I'm just confirming that two password fields match. Now my requirement is that I have to confirm email address too. I know this is a silly requirement, but I can't change it. So now the class definition will look like this:
@FieldMatch.List({
@FieldMatch(first = "password", second = "passwordConfirmation")
@FieldMatch(first = "email", second = "emailConfirmation")
})
public class RegistrationForm {
// ...
}
Clearly, my current resource bundle will not work because I need two separate messages (one for each match). I've tried using message="{emails.must.match}"
and then defining that message in the resource bundle, but it never actually shows the message, it just shows the actual text {email.must.match}
.
So after all that explaining, my question is simple: How can I make each one of those FieldMatch
validators at the class level have a different message that is defined in a resource bundle so it can be localized?
Thanks in advance for any help!
[EDIT] For those curious, this is the FieldMatch
validator that I'm using.
UPDATE 5/23/2012 Here's the bean definition others have asked for:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>ErrorBundle</value>
<value>ForgotPasswordBundle</value>
<!-- etc etc -->
</list>
</property>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>