0

I wrote a custom Hibernate validation constraint for Money class:

@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = MoneyLimitedValidator.class)
@Documented
public @interface MoneyLimited {
    String message() default "{error.validation.money.limited}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

It works fine except error message. I see very strange behaviour: resource bundle found and message resolved by name, but it wrapped into special chars which usually appears if message can't be resolved by name:

??Incorrect sum value._en_EN??

Here Incorrect sum value. is a correct message, which is accepted by name error.validation.money.limited. Originaly my message looks so:

error.validation.money.limited = Incorrect sum value.

I tried to remove {} braces from message name into MoneyLimited#message(), but nothing changes (even more strange).

I specified my validation message bundle as described in this answer:

<annotation-driven validator="validator" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource" ref="messageSource"/>
</bean>

So the question is: how to fix message?

I'm using Spring Framework 3.2.4.RELEASE and Hibernate Validator 4.3.1.Final.

Community
  • 1
  • 1
Maxim Kolesnikov
  • 5,075
  • 6
  • 38
  • 68
  • http://stackoverflow.com/questions/5550065/hibernate-validation-and-localized-error-messages?rq=1 can help? Or you can put breakpoint [here](http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-validator/4.3.1.Final/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java#ResourceBundleMessageInterpolator) – Luca Basso Ricci Aug 10 '13 at 20:38
  • No, it doesn't help, because it do actually the same. Look at this answer: http://stackoverflow.com/questions/11225023/messageinterpolator-in-spring#11226683 – Maxim Kolesnikov Aug 10 '13 at 21:58
  • (i'm just speculating): seems messageresolver try to resolve errormessage twice: first time with success, after appending locale(or default locale) and unsucessfully (the reason of ??) (maybe is a issue with localization) – Luca Basso Ricci Aug 10 '13 at 22:11
  • You need for sure the curly braces. They are marking the string as message parameter. Without no interpolation will take place. What's your full technology stack? Are you using JSF as well? Which component does return you the error message? – Hardy Aug 11 '13 at 08:51
  • Thanks, Hardy, but I'm actually solved my problem few hours ago. To get a correct message I should to remove braces. I know that I wrote it doesn't helps above. But when I did this change, I didn't restarted my app (I'm using *JRebel*). I thought it would automatically loads my changes, but it didn't. Looks like *JRebel* doesn't works with `@inteface`. – Maxim Kolesnikov Aug 11 '13 at 10:17

1 Answers1

0

I'm found the reason of double resolving. I didn't mentioned before, that I'm using Thymeleaf as template engine (which is using SpringEL). There are an useful snippet in example app, which I just copy-paste (shame on me) and forgot about:

<div class="errors" th:if="${#fields.hasErrors('*')}" th:fragment="validationErrorTpl">
    <ul>
        <li th:each="err : ${#fields.errors('*')}" th:text="#{${err}}">Input is incorrect</li>
    </ul>
</div>

As you can see ${err} variable enclosed in #{}, which is actually resolving message from bundle. So with braces in validation constraint, message was resolved twice: on annotation level and in view template.

Maxim Kolesnikov
  • 5,075
  • 6
  • 38
  • 68