I have following bean:
class CampaignBeanDto {
@Future
Date startDate;
Date endDate;
...
}
Obviously I that endDate
should be after startDate. I want to validate it.
I know that I can manually realize annotation for @FutureAfterDate
, validator for this and initialize threshold date manually but I want to use @Validated
spring mvc annotation.
How can I achieve it?
To check if a date is in the future:Use the Date() constructor to get the current date. Optionally set the time of the current date to the last millisecond. Check if the date is greater than the current date.
Steps to Create Date Validation with Date Range From here in the data validation dialog box, select “Date” from the “Allow” drop-down. After that, select between from the “Data” drop-down. Next, you need to enter two dates in the “Start Date” and “End Date” input boxes. In the end, click OK.
You're gonna have to bear down and write yourself a Validator.
This should get you started:
Cross field validation with Hibernate Validator (JSR 303)
You should not use Annotations for cross field validation, write a validating 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 simply call the validator function when first receiving the data:
if (Boolean.FALSE.equals(incomingData.validate())) {
response = ResponseEntity.status(422).body(UNPROCESSABLE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With