Hi,
I want to apply validation rules to our business objects, and since it makes sense to report problems to the caller as soon as possible (instead of waiting until they have assembled the whole object), I would like to invoke each single-property validator (e.g. @NotEmpty) when the setter of that property is called.
I don't want an AOP proxy just for this, I'm OK with triggering the validation manually from the setter, but I'd to use Hibernate Validator's annotations.
Examples:
Code:
// Version 1: looks nice, but the validation doesn't get invoked when the setter is called.
@NotEmpty
public String getName() {
return name;
}
public String setName(String name) {
this.name = name;
}
Code:
// Version 2: the validation takes place instantly, but it doesn't use the Hibernate Validator framework.
public String getName() {
return name;
}
public String setName(String name) {
if (name == null || name.length() == 0) {
// report problem
}
this.name = name;
}
Code:
// Version 3: I need something like this
@NotNull
public String getName() {
return name;
}
public String setName(String name) {
// Run validation based on object type, property name & property value
ValidatorManager.validate(this.getClass(), "name", name);
this.name = name;
}
How can I do this?
Thanks,
Peter