Package com.oorian.validation.validators
Class CompareValidator
java.lang.Object
com.oorian.validation.validators.CompareValidator
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumComparison operations supported by the validator. -
Constructor Summary
ConstructorsConstructorDescriptionCompareValidator(String field1Name, String field2Name, CompareValidator.Operation operation) Creates a CompareValidator for the specified fields and operation. -
Method Summary
Modifier and TypeMethodDescriptionReturns the name of the first field.Returns the name of the second field.Returns the error message for this validator.Returns the comparison operation.validate(ValidationContext context) Validates the form using all field values from the context.withMessage(String message) Sets a custom error message.
-
Constructor Details
-
CompareValidator
Creates a CompareValidator for the specified fields and operation.- Parameters:
field1Name- The name of the first fieldfield2Name- The name of the second fieldoperation- The comparison operation
-
-
Method Details
-
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
-
getField1Name
Returns the name of the first field.- Returns:
- The first field name
-
getField2Name
Returns the name of the second field.- Returns:
- The second field name
-
getOperation
Returns the comparison operation.- Returns:
- The operation
-