views:

496

answers:

4

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.

A: 

Since there is no way to access the bean in a JSR 303 validator, this is not possible.

A workaround would be to supply your own ConstraintValidatorContext and extend it with a reference to the bean you're currently validating. But I'm not sure whether you can modify/override the respective factory.

Aaron Digulla
+1  A: 

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;
  }
McDowell
+2  A: 

The answer is a class level validator. You can define custom constraints which you can place on entity class. At validation time you get passed the whole instance into the isValid() method. You can then compare your two dates against each other. See also the Hibernate Validator documentation.

Hardy