In the previous article, we have seen custom constraints. In this article we will see about validation groups.
Validation Groups:
Bean validation uses validation groups to determine what and when validation occurs. A validation group contains one or more constraints. There are no special interfaces to implement or annotations to apply in order to create a validation group. A validation group is denoted simply by a class definition. However, it is strongly recommended that simple interfaces are used. This is a best practice since it makes validation groups more usable in multiple environments. Whereas, if a class or entity definition were used as a validation group, it may pollute the object model of another application by bringing in domain classes and logic that do not make sense for the application. By default, if a validation group or multiple groups is not specified on an individual constraint, it will be validated using the javax.validation.groups.Default group.
Another advantage with validation groups is, instead of validating all the constraints at once, it will validate one group after the other, it will stop validation if one group couldn't be validated successfully.
Let's see how it can be implemented,
package com.sample.bean; import java.util.Date; import javax.validation.constraints.Future; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Book { @NotNull(groups = BookNotNull.class, message="Book Name should not be null") @Size(min = 2, max = 50, message = "Name length should be between 2 and 50") private String name; @NotNull(groups = BookNotNull.class, message="Book Author should not be null") private String author; @NotNull(groups = BookNotNull.class, message="Book Edition should not be null") private String edition; @NotNull(groups = BookNotNull.class, message="Release date should not be null") @Future(message = "Release date should not be past date") private Date releaseDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getEdition() { return edition; } public void setEdition(String edition) { this.edition = edition; } public Date getReleaseDate() { return releaseDate; } public void setReleaseDate(Date releaseDate) { this.releaseDate = releaseDate; } public interface BookNotNull { } }In the above bean, @NotNull constraint is part of the BookNotNull validation group and @Size, @Future constraints belong to the Default group (which is specified by the JSR 303 API), as no special validation group is specified for them.
Now, we will test this using BookTest.java as shown below,
package com.bean.test; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.groups.Default; import com.sample.bean.Book; import com.sample.bean.Book.BookNotNull; public class BookTest { private static Validator validator; public static void main(String args[]) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); Calendar cal = Calendar.getInstance(); Book b = new Book(); b.setAuthor("Satish"); b.setEdition("first"); b.setName("j"); b.setReleaseDate(new Date("07/26/2011")); b.setRating(1); Set<ConstraintViolation<Book>> constraintViolations = validator.validate(b, BookNotNull.class, Default.class); int count = constraintViolations.size(); System.out.println("Total voilations: "+count); Iterator it = constraintViolations.iterator(); while(it.hasNext()){ ConstraintViolation cv=(ConstraintViolation)it.next(); System.out.println(cv.getMessage()); } } }Above, we have specified BookNotNull and Default groups in validate(). So, first only the constraints which are under BookNotNull group will be validated. If there is any violations, processing stops there(constraints under default group will not be validated). If no violations occur, then constraints under Default group will be validated.
If you specify only BookNotNull group in validate(), only constraints under BookNotNull group will be validated.