Spring MVC with Hibernate validator. How to check a property by groups?

There are two problems:

1.Sping / MVC use hibernate validater, custom validator, how to show message? for example: Cross-field validation using Hibernate Validator (JSR 303)

@FieldMatch.List({ @FieldMatch(fieldName="password",verifyName="passwordVerify",message="password confirm valid!", groups={Default.class}) }) @ScriptAssert(lang="javascript",script="_this.name.equals(_this.verifyCode)") public class LoginForm {...} 

How to show message in jsp with resource properties file?

NotEmpty.loginForm.name = "username cannot be empty!" NotEmpty.loginForm.password = "Password cannot be empty!"

2. I want to use a group validator with spring mvc, for example, one user form for login and registration

  @FieldMatch.List({ @FieldMatch(fieldName="password",verifyName="passwordVerify",message="password confirm valid!", groups={Default.class}) })@ScriptAssert(lang="javascript",script="_this.name.equals(_this.verifyCode)",groups={Default.class,LoginChecks.class,RegisterChecks.class}) public class LoginForm { @NotEmpty(groups={Default.class,LoginChecks.class,RegisterChecks.class}) @Size(min=3,max=10,groups={LoginChecks.class,RegisterChecks.class}) private String name; @NotEmpty(groups={Default.class,LoginChecks.class,RegisterChecks.class}) @Size(max=16,min=5,groups={LoginChecks.class,RegisterChecks.class}) private String password; private String passwordVerify; @Email(groups={Default.class,LoginChecks.class,RegisterChecks.class}) private String email; private String emailVerify; ... } 

Controller parameter annotations: @valid, any support group support annotations by group?

First post :)

+8
spring-mvc hibernate-validator bean-validation
source share
4 answers

Update:

Spring 3.1 provides @Validated annotation, which you can use as a replacement for @Valid, and accept groups. If you are using Spring 3.0.x, you can still use the code in this answer.

Original answer:

This is definitely a problem. Since the @Valid annotation does not support groups, you will have to do the verification yourself. Here is the method we wrote to check and map errors to the correct path in BindingResult. It will be a good day when we get the @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 || classes[0] == null ) { classes = new Class<?>[] { Default.class }; } Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); Set<ConstraintViolation<Object>> violations = validator.validate( o, classes ); for ( ConstraintViolation<Object> 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 post regarding our solution. http://digitaljoel.nerd-herders.com/2010/12/28/spring-mvc-and-jsr-303-validation-groups/

+2
source share

Regarding support for confirmation groups inside the @Valid annotation - there is a way to do this, which I recently found, it overrides the default group for verified beans:

 @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 use @Valid , but keeping order with validation groups.

+1
source share

Starting with Spring 3.1, you can get group-based confirmation using Spring @Validated as follows:

 @RequestMapping public String doLogin(@Validated({LoginChecks.class}) LoginForm) { // ... } 

@Validated is a replacement for @Valid .

+1
source share

Controller parameter annotations: @valid, any support group support annotations by group?

Now it is possible with the annotation @ConvertGroup. Check this out http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-group-conversion

+1
source share

All Articles