org.apache.myfaces.custom.comparetovalidator
Class AbstractCompareToValidator

java.lang.Object
  extended by org.apache.myfaces.validator.ValidatorBase
      extended by org.apache.myfaces.custom.comparetovalidator.AbstractCompareToValidator
All Implemented Interfaces:
java.util.EventListener, javax.faces.component.StateHolder, javax.faces.validator.Validator

public abstract class AbstractCompareToValidator
extends org.apache.myfaces.validator.ValidatorBase

Validates this component against another component.

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

Valid operator attribute values:

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

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:

Version:
$Revision: 940142 $ $Date: 2010-05-01 20:36:31 -0500 (Sat, 01 May 2010) $
Author:
Mike Kienenberger (latest modification by $Author: lu4242 $)

Field Summary
static java.lang.String COMPARE_TO_MESSAGE_ID
          The message identifier of the FacesMessage to be created if the comparison check fails.
static java.lang.String OPERATOR_EQUALS
           
static java.lang.String OPERATOR_EQUALS_ALT
           
static java.lang.String OPERATOR_EQUALS_ALT2
           
static java.lang.String OPERATOR_GREATER_THAN
           
static java.lang.String OPERATOR_GREATER_THAN_ALT
           
static java.lang.String OPERATOR_GREATER_THAN_OR_EQUALS
           
static java.lang.String OPERATOR_GREATER_THAN_OR_EQUALS_ALT
           
static java.lang.String OPERATOR_LESS_THAN
           
static java.lang.String OPERATOR_LESS_THAN_ALT
           
static java.lang.String OPERATOR_LESS_THAN_OR_EQUALS
           
static java.lang.String OPERATOR_LESS_THAN_OR_EQUALS_ALT
           
static java.lang.String OPERATOR_NOT_EQUALS
           
static java.lang.String OPERATOR_NOT_EQUALS_ALT
           
static java.lang.String VALIDATOR_ID
          The standard converter id for this converter.
 
Fields inherited from interface javax.faces.validator.Validator
NOT_IN_RANGE_MESSAGE_ID
 
Constructor Summary
AbstractCompareToValidator()
           
 
Method Summary
protected  java.util.Comparator createComparator()
           
protected  javax.faces.convert.Converter findUIOutputConverter(javax.faces.context.FacesContext facesContext, javax.faces.component.UIComponent component)
           
abstract  java.lang.String getAlternateOperatorName()
          custom operator name in error message (ie "after" instead of "greater than" for dates)
protected  java.lang.String getClassCastExceptionMessage(java.lang.String name, java.lang.Class clazz, java.lang.Object object)
           
abstract  java.lang.Object getComparator()
          Value binding for an alternate java.util.Comparator object if component values don't implement Comparable
protected  java.lang.Object getConvertedValueNonValid(javax.faces.context.FacesContext facesContext, javax.faces.component.UIComponent component)
           
abstract  java.lang.String getFor()
          The JSF id of the component with which to compare values.
abstract  java.lang.String getOperator()
          Operator for comparison: equals: eq, ==, =, not equals: ne, !=, greater than: gt, >, less than: lt, <, greater than or equals: ge, >=, less than or equals: le, <=
protected  java.lang.String getOperatorForString(java.lang.String operatorSpecified)
           
protected  javax.faces.render.Renderer getRenderer(javax.faces.context.FacesContext context, javax.faces.component.UIComponent foreignComponent)
           
protected  java.lang.String nameForOperator(java.lang.String operator)
           
abstract  void setAlternateOperatorName(java.lang.String alternateOperatorName)
           
abstract  void setComparator(java.lang.Object comparator)
           
abstract  void setFor(java.lang.String string)
           
abstract  void setOperator(java.lang.String operator)
           
 void validate(javax.faces.context.FacesContext facesContext, javax.faces.component.UIComponent uiComponent, java.lang.Object value)
           
protected  boolean validateOperatorOnComparisonResult(java.lang.String operator, int result)
           
 
Methods inherited from class org.apache.myfaces.validator.ValidatorBase
getDetailMessage, getFacesContext, getFacesMessage, getMessage, getStringValue, getSummaryMessage, getValueExpression, isTransient, restoreAttachedState, restoreState, saveAttachedState, saveState, setDetailMessage, setMessage, setSummaryMessage, setTransient, setValueExpression
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VALIDATOR_ID

public static final java.lang.String VALIDATOR_ID

The standard converter id for this converter.

See Also:
Constant Field Values

COMPARE_TO_MESSAGE_ID

public static final java.lang.String COMPARE_TO_MESSAGE_ID

The message identifier of the FacesMessage to be created if the comparison check fails.

See Also:
Constant Field Values

OPERATOR_EQUALS

public static final java.lang.String OPERATOR_EQUALS
See Also:
Constant Field Values

OPERATOR_NOT_EQUALS

public static final java.lang.String OPERATOR_NOT_EQUALS
See Also:
Constant Field Values

OPERATOR_GREATER_THAN

public static final java.lang.String OPERATOR_GREATER_THAN
See Also:
Constant Field Values

OPERATOR_LESS_THAN

public static final java.lang.String OPERATOR_LESS_THAN
See Also:
Constant Field Values

OPERATOR_GREATER_THAN_OR_EQUALS

public static final java.lang.String OPERATOR_GREATER_THAN_OR_EQUALS
See Also:
Constant Field Values

OPERATOR_LESS_THAN_OR_EQUALS

public static final java.lang.String OPERATOR_LESS_THAN_OR_EQUALS
See Also:
Constant Field Values

OPERATOR_EQUALS_ALT

public static final java.lang.String OPERATOR_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_NOT_EQUALS_ALT

public static final java.lang.String OPERATOR_NOT_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_GREATER_THAN_ALT

public static final java.lang.String OPERATOR_GREATER_THAN_ALT
See Also:
Constant Field Values

OPERATOR_LESS_THAN_ALT

public static final java.lang.String OPERATOR_LESS_THAN_ALT
See Also:
Constant Field Values

OPERATOR_GREATER_THAN_OR_EQUALS_ALT

public static final java.lang.String OPERATOR_GREATER_THAN_OR_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_LESS_THAN_OR_EQUALS_ALT

public static final java.lang.String OPERATOR_LESS_THAN_OR_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_EQUALS_ALT2

public static final java.lang.String OPERATOR_EQUALS_ALT2
See Also:
Constant Field Values
Constructor Detail

AbstractCompareToValidator

public AbstractCompareToValidator()
Method Detail

getOperatorForString

protected java.lang.String getOperatorForString(java.lang.String operatorSpecified)

nameForOperator

protected java.lang.String nameForOperator(java.lang.String operator)

validateOperatorOnComparisonResult

protected boolean validateOperatorOnComparisonResult(java.lang.String operator,
                                                     int result)

validate

public void validate(javax.faces.context.FacesContext facesContext,
                     javax.faces.component.UIComponent uiComponent,
                     java.lang.Object value)
              throws javax.faces.validator.ValidatorException
Throws:
javax.faces.validator.ValidatorException

getClassCastExceptionMessage

protected java.lang.String getClassCastExceptionMessage(java.lang.String name,
                                                        java.lang.Class clazz,
                                                        java.lang.Object object)

createComparator

protected java.util.Comparator createComparator()

getFor

public abstract java.lang.String getFor()
The JSF id of the component with which to compare values.

Returns:
the foreign component_id, on which a value should be validated

setFor

public abstract void setFor(java.lang.String string)
Parameters:
string - the foreign component_id, on which a value should be validated

getOperator

public abstract java.lang.String getOperator()
Operator for comparison: equals: eq, ==, =, not equals: ne, !=, greater than: gt, >, less than: lt, <, greater than or equals: ge, >=, less than or equals: le, <=

Returns:

setOperator

public abstract void setOperator(java.lang.String operator)

getComparator

public abstract java.lang.Object getComparator()
Value binding for an alternate java.util.Comparator object if component values don't implement Comparable

Returns:

setComparator

public abstract void setComparator(java.lang.Object comparator)

getAlternateOperatorName

public abstract java.lang.String getAlternateOperatorName()
custom operator name in error message (ie "after" instead of "greater than" for dates)

Returns:

setAlternateOperatorName

public abstract void setAlternateOperatorName(java.lang.String alternateOperatorName)

getRenderer

protected javax.faces.render.Renderer getRenderer(javax.faces.context.FacesContext context,
                                                  javax.faces.component.UIComponent foreignComponent)

findUIOutputConverter

protected javax.faces.convert.Converter findUIOutputConverter(javax.faces.context.FacesContext facesContext,
                                                              javax.faces.component.UIComponent component)

getConvertedValueNonValid

protected java.lang.Object getConvertedValueNonValid(javax.faces.context.FacesContext facesContext,
                                                     javax.faces.component.UIComponent component)
                                              throws javax.faces.convert.ConverterException
Throws:
javax.faces.convert.ConverterException


Copyright © 2011 The Apache Software Foundation. All Rights Reserved.