Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display only one error message per field with JSR 303

I have multiple validation annotation on each filed of my bean, but I want to display only first failed message for each field in my spring MVC application where I am using JSR 303. Currently, it is evaluating all the constraints and displaying all the messages messages for each field. When a filed is empty, I dont want to evaluate its min and max constraint.

I have tried with groups, but it is not working as expected and more over I need to invoke the validator explicitly something like validator.validate() which I want to avoid.

IS there any alternative to achieve this by over writing some validation classes?

I have at least 3 constraints for each field and maximum 5. Displaying all theses messages for each field is not at all looking good. I am suing spring 4.x and hibernate 4.x

like image 211
user1614862 Avatar asked May 20 '14 20:05

user1614862


1 Answers

The way to go is via groups and group sequences. That's the only portable feature which ensures an validation order and termination of validation if a constraint within a group fails. Can you not assign the constraints in your bean to appropriate groups and then redefine the default groups sequence for the bean itself? See also http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-default-group-class

like image 85
Hardy Avatar answered Oct 02 '22 18:10

Hardy