Reputation: 13
public class User {
@NotNull(message="Age is Required")
@Min(value = 18, message = "Age must be greater than or equal to 18")
@Max(value = 150, message = "Age must be less than or equal to 150")
private Integer age;
}
If the User enters "String Value" in age, I want to validate them and should provide error message "Age is Invalid".
@OnlyNumber(message = "Age is Invalid") //Like This
Which Annotation helps in validating this kind of Type Validation, Similarly if the use enters different type format instead of Date. How do we handle them and provide custom error message.
Upvotes: 0
Views: 1667
Reputation: 2931
There is no Hibernate validation for that because exception is thrown when Spring tries to bind String value with Integer field.
What you can do if you want to display customized message for that exception is configure Spring to use message.properties file and specify 'typeMismatch.user.age' message or global message for typeMismatch.
For example (Spring 3.2 and Hibernate Validator 4.3), in servlet-config.xml
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:messages/messages">
</bean>
In src/main/resources/messages/messages.properties
typeMismatch = Invalid data format!
Upvotes: 1