Package com.oorian.validation.validators
Class DateRangeValidator
java.lang.Object
com.oorian.validation.validators.DateRangeValidator
- All Implemented Interfaces:
FormValidator
Form-level validator that validates date ranges across two fields.
DateRangeValidator implements FormValidator to provide cross-field date validation. It compares two date fields to ensure they form a valid range (e.g., start date before end date).
Common use cases include:
- Event start/end dates
- Travel departure/return dates
- Project timeline dates
- Reservation check-in/check-out dates
Usage:
// Basic start before end validation
DateRangeValidator dateRange = new DateRangeValidator("startDate", "endDate", "yyyy-MM-dd")
.requireStartBeforeEnd()
.withMessage("Start date must be before end date");
// Allow same day (start <= end)
DateRangeValidator sameDayOk = new DateRangeValidator("startDate", "endDate", "yyyy-MM-dd")
.requireStartBeforeOrEqualEnd()
.withMessage("Start date must be on or before end date");
// Maximum range of 30 days
DateRangeValidator maxRange = new DateRangeValidator("startDate", "endDate", "yyyy-MM-dd")
.requireStartBeforeEnd()
.maxDaysBetween(30)
.withMessage("Date range cannot exceed 30 days");
// Add to ValidatedForm
validatedForm.addFormValidator(dateRange);
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDateRangeValidator(String startFieldName, String endFieldName) Creates a DateRangeValidator for the specified fields with default format (yyyy-MM-dd).DateRangeValidator(String startFieldName, String endFieldName, String format) Creates a DateRangeValidator for the specified fields with a custom format. -
Method Summary
Modifier and TypeMethodDescriptionReturns the name of the end date field.Returns the error message for this validator.Returns the name of the start date field.maxDaysBetween(int days) Sets the maximum number of days allowed between start and end dates.minDaysBetween(int days) Sets the minimum number of days required between start and end dates.Requires that start date is strictly before end date.Requires that start date is on or before end date (same day allowed).validate(ValidationContext context) Validates the form using all field values from the context.withMessage(String message) Sets a custom error message.
-
Constructor Details
-
DateRangeValidator
Creates a DateRangeValidator for the specified fields with default format (yyyy-MM-dd).- Parameters:
startFieldName- The name of the start date fieldendFieldName- The name of the end date field
-
DateRangeValidator
Creates a DateRangeValidator for the specified fields with a custom format.- Parameters:
startFieldName- The name of the start date fieldendFieldName- The name of the end date fieldformat- The date format pattern (e.g., "yyyy-MM-dd", "MM/dd/yyyy")
-
-
Method Details
-
requireStartBeforeEnd
Requires that start date is strictly before end date.- Returns:
- This validator for method chaining
-
requireStartBeforeOrEqualEnd
Requires that start date is on or before end date (same day allowed).- Returns:
- This validator for method chaining
-
maxDaysBetween
Sets the maximum number of days allowed between start and end dates.- Parameters:
days- The maximum number of days- Returns:
- This validator for method chaining
-
minDaysBetween
Sets the minimum number of days required between start and end dates.- Parameters:
days- The minimum number of days- Returns:
- This validator for method chaining
-
withMessage
Sets a custom error message.- Parameters:
message- The error message- Returns:
- This validator for method chaining
-
validate
Description copied from interface:FormValidatorValidates the form using all field values from the context.- Specified by:
validatein interfaceFormValidator- Parameters:
context- The validation context containing all form field values- Returns:
- The validation result
-
getMessage
Returns the error message for this validator.- Returns:
- The error message
-
getStartFieldName
Returns the name of the start date field.- Returns:
- The start field name
-
getEndFieldName
Returns the name of the end date field.- Returns:
- The end field name
-