While it's not widely known, Java Bean Validation ships with the validateProperty() and validateValue() methods, which can be used for validating the individual fields of a constrained class as well as values even before assigning them.
I assume that you already have at least a basic background on how to use the standard for validating objects, but if you want to get a more intimate understanding on how this works, feel free to check my previous article, where I took an in-depth look at Bean Validation's core features.
Selective Validation of Field-Level Constraints
In a typical use case, a constrained domain class like the following will be validated in one single step with a Bean Validation implementation, like Hibernate Validator.
public class User {
@NotEmpty(message = "Name may not be empty")
@Size(min = 2, max = 32,
message = "Name must be between 2 and 32 characters long")
private String name;
@NotEmpty(message = "Email may not be empty")
@Email(message = "Email must be a well-formed email address")
private String email;
public User(){}
public User(String name, String email) {
this.name = name;
this.email = email;
}
// setters and getters for name / email
}
But with validateProperty() and validateValue() the validation process can be performed more selectively.
The validateProperty() Method
Continue reading %Java Bean Validation’s validateProperty() and validateValue() in Three Minutes%
by Alejandro Gervasio via SitePoint
No comments:
Post a Comment