Project Documentation
Foundation

Description

Validate by comparing this component value to another component value

Screen Shot

Not Available

API

validatorId org.apache.myfaces.validator.CompareTo
validator-class org.apache.myfaces.custom.comparetovalidator.CompareToValidator
tag-class org.apache.myfaces.custom.comparetovalidator.ValidateCompareToTag

Usage

<t:inputCalendar id="startDate"/>
<t:inputCalendar id="endDate">
    <s:validateCompareTo operator="gt" for="startDate" />
    <s:validateCompareTo operator="gt" for="startDate" message="Start date must be before end date." />
    <s:validateCompareTo operator="gt" for="startDate" message="{0} must be {2} {3}" />
    <s:validateCompareTo operator="gt" for="startDate" alternateOperatorName="after" />
    <s:validateCompareTo operator="gt" for="startDate" message="{0} must be {2} {3}" alternateOperatorName="after" />
    <s:validateCompareTo operator="gt" for="startDate" comparator="#{dateComparator}" />
<t:inputCalendar>

Syntax

<s:validateCompareTo/>

for - Required reference to another component.
operator - Required operator for comparison:
....equals: eq, ==, =,
....not equals: ne, !=,
....greater than: gt, >,
....less than: lt, <,
....greater than or equals: ge, >=,
....less than or equals: le, <=
comparator - Optional value binding for an alternate java.util.Comparator object if component values don't implement Comparable
message - Optional alternate validation error message format string where
....{0} is the parent component id,
....{1} is the parent component value,
....{2} is the operator name,
....{3} is the foreign component id, and
....{4} is the foreign component value.
alternateOperatorName = Optional custom operator name in error message (ie "after" instead of "greater than" for dates)

Instructions

If the comparator attribute is specified, the component values are compared using the specified java.util.Comparator object. If no comparator is specified, the component values must implement Comparable and are compared using compareTo(). If either value or foreign value does not implement Comparable and no Comparator is specified, validation always succeeds.

Put this validator on the bottom-most component to insure that the foreign component's value has been converted and validated first. However, this validator will attempt to convert and validate the foreign component's value if this has not already occurred. This process may not be identical to the standard JSF conversion and validation process.

See "myfaces-example-sandbox/validateCompareTo.jsp" for an example.

Known issues

The validation error message key is currently hardcoded as "{0} value <{1}> must be {2} {3} value <{4}>" and should be localized.

Operator names should be localized.

Perhaps an exception should be thrown if the two values are not Comparable and no Comparator is specified.