Thursday, April 7, 2016

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.

public class User {

    @UserExists
    private String username;

}

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


@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:

@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:

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

Similar articles

In case you find this article interesting, have a look at my other blog posts:

Friday, March 4, 2016

Thursday, March 3, 2016

Tuesday, March 1, 2016

Quickstart: Angular2 with TypeScript and Gulp

Quickstart: Angular2 with TypeScript and Gulp

Angular2 is around the corner. The new version of the framework is much simpler to learn thanks to easier and more concise concepts like component-based architecture, new dependency injection or built-in modularity. In this step-by-step tutorial you will learn how how to get started with Angular2 with TypeScript and Gulp. Source code available on Github.

Friday, February 12, 2016

Sunday, January 31, 2016

Spring MVC 4 Quickstart Maven Archetype Improved - More Java 8 Features

For all those developers interested in bootstrapping Spring 4 application quickly without Spring Boot, please check my Spring MVC 4 Quickstart Maven Archetype that just got updated. Archetype uses Java 8 as target platform for some time already but no specific Java 8 features were supported. Recent changes bring (apart from some bug fixing) support for Java 8 Data & Time API in Thymeleaf, Jackson and JPA.

Wednesday, January 27, 2016