How can this date be confirmed in the future against a different date?

I have the following bean:

class CampaignBeanDto {

    @Future
    Date startDate;

    Date endDate;

    ...
}

      

Obviously what endDate

should be after startDate. I want to test it.

I know that I can manually implement an annotation for @FutureAfterDate

, a validator for this, and manually initialize the threshold date manually, but I want to use the @Validated

spring mvc annotation .

How can I achieve this?

+6


source to share


2 answers


You will have to carry and write yourself a validator.

This should help you:



Cross-field validation with Hibernate Validator (JSR 303)

+6


source


You shouldn't use annotations for validation between fields, write a validation function instead. Explained in this answer to the question, Cross-Field Validation with Hibernate Validator (JSR 303) .

For example, write a validator function like this:

public class IncomingData {

  @FutureOrPresent
  private Instant startTime;

  @Future
  private Instant endTime;

  public Boolean validate() {
      return startTime.isBefore(endTime);
  }
}

      



Then just call the validation function when you first get the data:

if (Boolean.FALSE.equals(incomingData.validate())) {
  response = ResponseEntity.status(422).body(UNPROCESSABLE);
}

      

+1


source







All Articles