Spring MVC with Hibernate Validator. How to validate property by group?

This is definitely a problem. Since the @Valid annotation doesn't support groups, you'll have to perform the validation yourself. Here is the method we wrote to perform the validation and map the errors to the correct path in the BindingResult.It'll be a good day when we get an @Valid annotation that accepts groups.

This is definitely a problem. Since the @Valid annotation doesn't support groups, you'll have to perform the validation yourself. Here is the method we wrote to perform the validation and map the errors to the correct path in the BindingResult.It'll be a good day when we get an @Valid annotation that accepts groups.

/** * Test validity of an object against some number of validation groups, or * Default if no groups are specified. * * @param result Errors object for holding validation errors for use in * Spring form taglib. Any violations encountered will be added * to this errors object.

* @param o Object to be validated * @param classes Validation groups to be used in validation * @return true if the object is valid, false otherwise. */ private boolean isValid( Errors result, Object o, Class... classes ) { if ( classes == null || classes. Length == 0 || classes0 == null ) { classes = new Class { Default.

Class }; } Validator validator = Validation. BuildDefaultValidatorFactory().getValidator(); Set> violations = validator. Validate( o, classes ); for ( ConstraintViolation v : violations ) { Path path = v.getPropertyPath(); String propertyName = ""; if ( path!

= null ) { for ( Node n : path ) { propertyName += n.getName() + ". "; } propertyName = propertyName. Substring( 0, propertyName.length()-1 ); } String constraintName = v.

GetConstraintDescriptor().getAnnotation().annotationType().getSimpleName(); if ( propertyName == null || "". Equals( propertyName )) { result. Reject( constraintName, v.getMessage()); } else { result.

RejectValue( propertyName, constraintName, v.getMessage() ); } } return violations.size() == 0; } I copied this source from my blog entry regarding our solution. http://digitaljoel.nerd-herders.com/2010/12/28/spring-mvc-and-jsr-303-validation-groups.

As for validation groups support inside @Valid annotation - there is a way to do it, that I've recently found, it's redefining default group for validated bean: @GroupSequence({TestForm. Class, FirstGroup. Class, SecondGroup.

Class}) class TestForm { @NotEmpty public String firstField; @NotEmpty(groups=FirstGroup. Class) public String secondField; //not validated when firstField validation fails @NotEmpty(groups=SecondGroup. Class) public String thirdField; //not validated when secondField validation fails } Now, you can still use @Valid, yet preserving order with validation groups.

Interesting solution. This will work if you want to always do all the validation, but if in some locations you only want to validate SecondGroup and in others you only want FirstGroup this won't help. – digitaljoel Oct 17 at 19:05 Yes, it has few pitfalls.

But in general, when it comes to more complex validation, with conditional and ordered validators, annotations fails miserable :( – Adam Jurczyk Oct 17 at 21:53.

This is definitely a problem. Since the @Valid annotation doesn't support groups, you'll have to perform the validation yourself. Here is the method we wrote to perform the validation and map the errors to the correct path in the BindingResult.

It'll be a good day when we get an @Valid annotation that accepts groups.

As for validation groups support inside @Valid annotation - there is a way to do it, that I've recently found, it's redefining default group for validated bean.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions