0

https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-message-interpolation.html

According to the hibernate validation documentation, the following can be done where {min} and {max} parameters are replaced in error message accordingly.

 @Size(
            min = 2,
            max = 14,
            message = "The license plate must be between {min} and {max} characters long"
    )

I am using this notation in a Spring application but these arguments are not replaced. I get back "The license plate must be between min and max characters long"

Do I need to configure the validator differently to make this work?

Here is my configuration:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/> 
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>

   <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>
led
  • 611
  • 4
  • 11
  • 18
  • Which version hibernate validator library you using? – Zahid M May 03 '14 at 18:57
  • Can you show your bean config? Are you using a custom MessageInterpolator? With the default message interpolator the parameters should be replaced. Spring has several customization points around message interpolation. It is important to see your full configuration. Also the context in which you are validating s important. – Hardy May 04 '14 at 10:25
  • I am using the latest version (5.1.0) – led May 04 '14 at 16:56

1 Answers1

0

I find the reason. It's caused by the property "useCodeAsDefaultMessage", we can't set it to "true",just use default value "false". It seems that if it is true, just get the messages from the properties file in the "basenames", and don't use the default message file in the classpath root path or "org/hibernate/validator"!

Rocky Hu
  • 1,326
  • 4
  • 17
  • 32