VRaptor2 offers a plugin which uses the hibernate validator to check automatically your business logics for validation errors.
In order to use the hibernate validator plugin we have to do the following:
If you are in doubt about how VRaptor2's validation in general works, please see tutorial first.
Open your vraptor.xml file and register the plugin:
<vraptor> ... <plugin>org.vraptor.plugin.hibernate.HibernateValidatorPlugin</plugin> .... </vraptor>
You write the validator annotations at the persistence fields. Basically you annotate your model classes like this:
@Entity public class User { @Id @GeneratedValue private Long id; //use validation annotations! @Length(min=3,max=100) private String name; @Email private String email; @Min(5) private String password; //getters and setters }
This fairly simple example uses @Email, @Min and @Length to annotate the validation rules. The user name should be between 3 and 100 characters, email should be a valid email address and the password should have at least 5 characters.
There are a lot more annotations, please refer to the documentation.
Now that we've implemented the validation rules, we have to tell VRaptor when/where to apply these validations.
Let's see an example:
@Component("user") public class NewUser { @Parameter private User user = new User(); //user should be validated automatically @Validate(fields={"user.name","user.email"}) public void add() { //add user to database } public User getUser() { this.user; } }
@Validate is one of VRaptor's annotation which has one parameter. The fields parameter indicates which fields of the user object we want to check BEFORE add() is executed.
VRaptor will automatically wrap the method and will validate user.name and user.email using the hibernate validator.
Please observe that we don't check the password field even using the @Min() annotation in the User class.
If you would like to validate the entire user object, you could write:
... @Validate(fields={"user.name", "user.email", "user.password"}). public void add() { //add user to database } ...
But for a model with a lot of attributes, it would be very cumbersome. That's why let's change the previous example just a little bit:
@Component("user") public class NewUser { @Parameter @Valid private User user = new User(); @Validate(fields={"user"}) public void add() { //add user to database } }
We've introduced another Hibernate annotation, @Valid, which was applied directly on the user object. Now VRaptor knows that we want to validate the entire object and we just have to indicate the object name: @Validate(fields="user"). Finished ... VRaptor will validate the user for us.
If an error happened VRaptor will redirect to user.add.invalid (and as you probably know, a message will be added to ValidationErros). This part is not different to what if we would use validateAdd().
Print the errors ... for example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> There were some validation problems: <div id="errors"> <ul> <c:forEach var="error" items="${errors.iterator}"> <li>${error.key}</li> </c:forEach> </ul> </div> </html>
errors is automatically outjected if an error occurs. That's it!
You can also validate your parameters, using the params attribute:
public static class PersonParamLogic { @Validate(params = { "person.id1" }) public void partial(Person p) { } @Validate(params = { "person" }) public void complete(Person p) { } }
Be careful and remember the the parameter name is the type name and not the actual variable name.
Using the hibernate validator in combination with VRaptor's plugin is an easy and powerful choice. Validation went totally to the meta model. You don't have to write any line of validation code, which is always repetitive and boring. This code has already been written a thousand times. Just tell what to validate and not how and concentrate on your "real" business code.