Enterprise Java

Spring MVC: Trgger manual validation of a form object

Sometimes it may be needed to use manual validation in Spring MVC @Controller. This is very simple with Spring’s org.springframework.validation.ValidationUtils class. Learn how to invoke a validator in two different scenarios.
 
 
 
 
 
 

Scenario 1 – invoke validation

In this scenario, I have a user form with username field. Username field is validated with custom validator in order to verify the existance in e.g. database.

1
2
3
4
5
6
public class User {
 
    @UserExists
    private String username;
 
}

In controller class I have a method that handles POST method of that object:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
@Autowired
private org.springframework.validation.Validator validator;
 
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String validate(@ModelAttribute User user, Errors errors) {
 
    ValidationUtils.invokeValidator(validator, user, errors);
 
    if (errors.hasErrors()) {
        // error, show errors to the user
    }
 
    // success, form is valid!
}

org.springframework.validation.ValidationUtils is a class for invoking a org.springframework.validation.Validator.

Please note that user parameter is followed by org.springframework.validation.Errors object. Spring initializes this object but it is empty and can be be passed to a invokeValidator method.

Scenario 2 – invoke validation with hints

In this scenario, user form gets a bit more complicated:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
@GroupSequence(value = {ValidationOrder.First.class, ValidationOrder.Second.class})
interface ValidationOrder {
    interface First {}
    interface Second {}
}
 
 
public class User {
 
    @UserExists(groups = ValidationOrder.First.class)
    @UserIsEntitledToDiscount(groups = ValidationOrder.Second.class)
    private String username;
 
}

Thanks to @GroupSequence I could decide about the order of validation. To trigger validation I need to pass an additional argument to invokeValidator method so the groups are correctly used:

1
ValidationUtils.invokeValidator(validator, user, errors, ValidationOrder.class);

Source code

The source code contains all three approaches, so that you can quickly compare them: https://github.com/kolorobot/spring-mvc-beanvalidation11-demo

Reference: Spring MVC: Trgger manual validation of a form object from our JCG partner Rafal Borowiec at the Codeleak.pl blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button