Class CompareValidator

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

public class CompareValidator extends Object implements FormValidator
Form-level validator that compares two field values.

CompareValidator implements FormValidator to provide cross-field comparison validation. It compares two fields using a specified operation (equals, not equals, less than, etc.).

Common use cases include:

  • Password confirmation matching
  • Email confirmation matching
  • Date range validation (start date before end date)
  • Numeric comparisons between fields

Usage:


 // Password confirmation
 CompareValidator passwordConfirm = new CompareValidator(
     "password", "confirmPassword", Operation.EQUALS)
     .withMessage("Passwords do not match");

 // Date range validation
 CompareValidator dateRange = new CompareValidator(
     "startDate", "endDate", Operation.LESS_THAN)
     .withMessage("Start date must be before end date");

 // Add to ValidatedForm
 validatedForm.addFormValidator(passwordConfirm);
 validatedForm.addFormValidator(dateRange);
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • CompareValidator

      public CompareValidator(String field1Name, String field2Name, CompareValidator.Operation operation)
      Creates a CompareValidator for the specified fields and operation.
      Parameters:
      field1Name - The name of the first field
      field2Name - The name of the second field
      operation - The comparison operation
  • Method Details

    • withMessage

      public CompareValidator 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
    • getField1Name

      public String getField1Name()
      Returns the name of the first field.
      Returns:
      The first field name
    • getField2Name

      public String getField2Name()
      Returns the name of the second field.
      Returns:
      The second field name
    • getOperation

      public CompareValidator.Operation getOperation()
      Returns the comparison operation.
      Returns:
      The operation