7

I have a simple class like this,

import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;

public class Form implements Serializable {
   @NotNull
   @Length(min = 2, max = 20)
   private String lastName;
}

I have messages.properties file in the classpath. It's then loaded via Spring bean as follows,

<bean name="validator"
      class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="resourceBundleLocator"/>
    </property>
</bean>

<bean name="resourceBundleLocator" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath*:messages.properties</value>
        </list>
    </property>
</bean>

All I want is to get error messages customized according to the bean validated. In other words I want to have each ConstraintViolation object return the error message I have defined in my property file instead of the default one. Is it possible to add message property with a value like this format {xxx.yyyy.zzzz} and refer that message from the messages.properties file ?

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<Form>> inputErrors = validator.validate(form); 

If I want to customize the default message for @NotNull and @Length, how can I do that?

My experience on Hibernate validation is zero, Any sample code or step by step guide would be appreciated.

1 Answer 1

4

You have this resource bundle file holding the message values.

I think you have to use the path to the i.e. @NotNull annotation to override the message by hand! like javax.validation.constraints.NotNull=Notnull error happened! or something like that!

Look here for that :

Hibernate Validator - The error message

Hope that helps

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

6 Comments

Yeah fritz, it's true, but I don't want to override the default messageTemplate path because I have many @NotNull(and of course other annotations) in my Form class. I want to add specific error messages for each property. Is there an easy way without implementing custom MessageInterpolator. I found couple of custom message interpolator implementations, but I don't understand any of these. If you can give me the simplest possible solution with a full source code !!.
And also I tried overriding message attribute of each validation annotation with a format like this {vvvv.ssss.rrrt.message}, but when I called the getMessage of ConstraintViolation it's printed as it is, not the message of it.
I see! the only way which comes to my mind is to create a custom validator which holds a message ! like @MyNotNull + the message?! or you could do something like this ` ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Car car = new Car(null); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); assertEquals(1, constraintViolations.size()); assertEquals("may not be null", constraintViolations.iterator().next().getMessage()); ` and intercept the message in the object!?
Ok, I may be crazy, and apologize for that.I have to do this anyway, Is there a way to get the messages(key and value pairs) defined in the messages.properties file? If so I can go for the second option you mentioned earlier.
Yeah ! don't mind thats a tricky task ! try that stackoverflow.com/questions/3372569/… what framework are you using. @Autowired private ApplicationContext context; context().getMessage("key", null, Locale.getDefault()); something like that could do the job if you are using i.e. spring!
|

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.