Mahmood Mousa
Mahmood Mousa

Reputation: 5

Hibernate Custom Validator Messages Spring MVC

I am trying to customize messages for Hibernate validation annotations (NOTNULL , NOTEMPTY ,...) so that the application is able to read them from .proparties file.

I tried the solutions from the answer here: use custom validation in hibernate hibernate validator

but nothing works for me i still get the deafult messages (may not be null , may not be empty , ....).

Code sample :

application context.xml :

<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>messages</value>
        </list>
    </property>
</bean>

user.java :

@NotNull
private String name;

messages.proparties :

user.name.notNull = incorrect name

Upvotes: 0

Views: 62

Answers (1)

Pooja Aggarwal
Pooja Aggarwal

Reputation: 1213

You need to mention the key name on your @NotNull annotation.

@NotNull(meassage="{user.name.notNull}")
private String name;

You can learn more from the link https://teamtreehouse.com/library/displaying-validation-messages

Upvotes: 1

Related Questions