Project Documentation
Foundation

Summary

Tag name: <s:validateCompareTo>
Validator class: org.apache.myfaces.custom.comparetovalidator.CompareToValidator
Tag class: org.apache.myfaces.custom.comparetovalidator.ValidateCompareToTag
validator id: org.apache.myfaces.validator.CompareTo
Validates this component against another component.

Specify the foreign component with the for={foreign-component-id} attribute.

Valid operator attribute values:

  • equals: eq, ==, =,
  • not equals: ne, !=,
  • greater than: gt, >,
  • less than: lt, <,
  • greater than or equals: ge, >=,
  • less than or equals: le, <=

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.

The validation error message key is currently hardcoded as

"{0} value <{1}> must be {2} {3} value <{4}>"

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.

The alternateOperatorName attribute can specify a custom operator name. For example, use "after" instead of "greater than" when comparing dates.

The message attribute can specify an alternate validation error message key. For example, use "{0} must be {2} {3}" to remove values from the message.

Known issues:

  • Operator names should be localized.
  • The default message key should be localized.
  • Perhaps an exception should be thrown if the two values are not Comparable and no Comparator is specified.

Usage

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

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.

Attributes

Name Type Supports EL? Description
alternateOperatorName String Yes custom operator name in error message (ie "after" instead of "greater than" for dates)
comparator Object Yes Value binding for an alternate java.util.Comparator object if component values don't implement Comparable
detailMessage String Yes alternate validation error detail message format string (use 'message' and 'detailMessage' alternatively)
for String Yes The JSF id of the component with which to compare values.
message String Yes alternate validation error detail message format string (use 'message' and 'detailMessage' alternatively)
operator String Yes Operator for comparison: equals: eq, ==, =, not equals: ne, !=, greater than: gt, >, less than: lt, <, greater than or equals: ge, >=, less than or equals: le, <=
summaryMessage String Yes alternate validation error summary message format string