Class DateRangeValidator

java.lang.Object
com.oorian.validation.validators.DateRangeValidator
All Implemented Interfaces:
FormValidator

public class DateRangeValidator extends Object implements 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 Details

    • DateRangeValidator

      public DateRangeValidator(String startFieldName, String endFieldName)
      Creates a DateRangeValidator for the specified fields with default format (yyyy-MM-dd).
      Parameters:
      startFieldName - The name of the start date field
      endFieldName - The name of the end date field
    • DateRangeValidator

      public DateRangeValidator(String startFieldName, String endFieldName, String format)
      Creates a DateRangeValidator for the specified fields with a custom format.
      Parameters:
      startFieldName - The name of the start date field
      endFieldName - The name of the end date field
      format - The date format pattern (e.g., "yyyy-MM-dd", "MM/dd/yyyy")
  • Method Details

    • requireStartBeforeEnd

      public DateRangeValidator requireStartBeforeEnd()
      Requires that start date is strictly before end date.
      Returns:
      This validator for method chaining
    • requireStartBeforeOrEqualEnd

      public DateRangeValidator requireStartBeforeOrEqualEnd()
      Requires that start date is on or before end date (same day allowed).
      Returns:
      This validator for method chaining
    • maxDaysBetween

      public DateRangeValidator maxDaysBetween(int days)
      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

      public DateRangeValidator minDaysBetween(int days)
      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

      public DateRangeValidator withMessage(String message)
      Sets a custom error message.
      Parameters:
      message - The error message
      Returns:
      This validator for method chaining
    • validate

      public ValidationResult validate(ValidationContext context)
      Description copied from interface: FormValidator
      Validates the form using all field values from the context.
      Specified by:
      validate in interface FormValidator
      Parameters:
      context - The validation context containing all form field values
      Returns:
      The validation result
    • getMessage

      public String getMessage()
      Returns the error message for this validator.
      Returns:
      The error message
    • getStartFieldName

      public String getStartFieldName()
      Returns the name of the start date field.
      Returns:
      The start field name
    • getEndFieldName

      public String getEndFieldName()
      Returns the name of the end date field.
      Returns:
      The end field name