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.

5
  • stackoverflow.com/questions/5550065/… can help? Or you can put breakpoint here Commented Aug 10, 2013 at 20:38
  • No, it doesn't help, because it do actually the same. Look at this answer: stackoverflow.com/questions/11225023/… Commented Aug 10, 2013 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) Commented Aug 10, 2013 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? Commented Aug 11, 2013 at 8: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. Commented Aug 11, 2013 at 10:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.