There are two problems:
1.Sping/MVC use hibernate validater, Custom validater how to show message?
like: Cross field validation with 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 the message in jsp with resource property file?
NotEmpty.loginForm.name="username can not be empty!"
NotEmpty.loginForm.password="password can not be empty!"
2. I want to use group validater with spring mvc, like one userform for login and register
@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 annotation is @valid, any annotation support group validater by group?
First post 🙂
Best Answer
Update:
Spring 3.1 provides the @Validated annotation which you can use as a drop-in replacement for @Valid, and it accepts 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 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.
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/