0

I am trying to validate my form Data using Hibernate Validator in my Spring MVC application. I have a requirement where i need to check user input for predefined values which will not be allowed in the system.

for e.g if the value of firstName field is firstName, i want to fail the validation and tell user that this value is not allowed.

Can any one tell me how this can be achieved using hibernate validation

2 Answers 2

3

You can create a custom validator that implements ConstraintValidator. Please read this link: Hibernate validator

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

Comments

3

You can also use @Pattern annotation on top of your field declaration. With this annotation you need to specify the java regex pattern that will match with the value input by user and if it fails you can show the proper validation message to the user in jsp.

Hope this helps you. Cheers.

2 Comments

Thanks, but @Pattern cannot work with integer , e.g. @Pattern(regexp="^[79]$") private int location; in this case it will throw exception javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'java.lang.Integer'. Check configuration for 'location'
and if want to know how to support integer, please see stackoverflow.com/questions/26772748/…

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.