Say I have the following simple java bean:
class MyBean {
private Date startDate;
private Date endDate;
//setter, getters etc...
}
Is there a mechanism in JSR 303 to create a custom validator that validates the constraint that startDate must be before endDate?
It seems to me to be a common use case, yet I cannot find any examples of this kind of multiple property relationsship constraint.
I can think of a few things to try.
You could create a Constraint
with a target of the type itself with an appropriate validator:
@ValidateDateRange(start="startDate", end="endDate")
public class MyBean {
You could encapsulate a date range in a type and validate that:
public class DateRange {
private long start;
private long end;
public void setStart(Date start) {
this.start = start.getTime();
}
// etc.
You could add a simple property that performs the check:
public class MyBean {
private Date startDate;
private Date endDate;
@AssertTrue public boolean isValidRange() {
// TODO: null checks
return endDate.compareTo(startDate) >= 0;
}