Posts

Showing posts from August, 2014

Validation groups in Spring MVC

Image
All constraints in Bean Validation may be added to one or more groups via groups attribute. This allows you to restrict the set of constraints applied during validation. It can be handy in cases where some groups should be validated before others like e.g. in wizards. As of Spring MVC 3.1, automatic validation utilizing validation groups is possible with org.springframework.validation.annotation.Validated annotation. In this article I will use simple Spring MVC application to demonstrate how easily you can use validation groups to validate Spring’s MVC model attributes. Form Let’s start with the form class that will be validated in steps. Firstly, we define interfaces that represents constraint groups: public class Account implements PasswordAware { interface ValidationStepOne { // validation group marker interface } interface ValidationStepTwo { // validation group marker interface } } Validation contraints Next we assign constra

Spring MVC Integration Testing: Assert the given model attribute(s) have global errors

Image
In order to report a global error in Spring MVC using Bean Validation we can create a custom class level constraint annotation. Global errors are not associated with any specific fields in the validated bean. In this article I will show how to write a test with Spring Test that verifies if the given model attribute has global validation errors.