org.apache.myfaces.commons.validator
Class AbstractCompareToValidator

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

@JSFValidator(name="mcv:validateCompareTo",
              clazz="org.apache.myfaces.commons.validator.CompareToValidator",
              tagClass="org.apache.myfaces.commons.validator.ValidateCompareToTag",
              serialuidtag="-8879289182242196266L")
public abstract class AbstractCompareToValidator
extends 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: 1021618 $ $Date: 2010-10-11 23:08:53 -0500 (Mon, 11 Oct 2010) $
Author:
Mike Kienenberger (latest modification by $Author: lu4242 $)

Field Summary
static String COMPARE_TO_MESSAGE_ID
          The message identifier of the FacesMessage to be created if the comparison check fails.
static String OPERATOR_EQUALS
           
static String OPERATOR_EQUALS_ALT
           
static String OPERATOR_EQUALS_ALT2
           
static String OPERATOR_GREATER_THAN
           
static String OPERATOR_GREATER_THAN_ALT
           
static String OPERATOR_GREATER_THAN_OR_EQUALS
           
static String OPERATOR_GREATER_THAN_OR_EQUALS_ALT
           
static String OPERATOR_LESS_THAN
           
static String OPERATOR_LESS_THAN_ALT
           
static String OPERATOR_LESS_THAN_OR_EQUALS
           
static String OPERATOR_LESS_THAN_OR_EQUALS_ALT
           
static String OPERATOR_NOT_EQUALS
           
static String OPERATOR_NOT_EQUALS_ALT
           
static 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  Comparator createComparator()
           
abstract  String getAlternateOperatorName()
          custom operator name in error message (ie "after" instead of "greater than" for dates)
protected  String getClassCastExceptionMessage(String name, Class clazz, Object object)
           
abstract  Object getComparator()
          Value binding for an alternate java.util.Comparator object if component values don't implement Comparable
abstract  String getFor()
          The JSF id of the component with which to compare values.
abstract  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  String getOperatorForString(String operatorSpecified)
           
protected  String nameForOperator(String operator)
           
abstract  void setAlternateOperatorName(String alternateOperatorName)
           
abstract  void setComparator(Object comparator)
           
abstract  void setFor(String string)
           
abstract  void setOperator(String operator)
           
 void validate(javax.faces.context.FacesContext facesContext, javax.faces.component.UIComponent uiComponent, Object value)
           
protected  boolean validateOperatorOnComparisonResult(String operator, int result)
           
 
Methods inherited from class org.apache.myfaces.commons.validator.ValidatorBase
getDetailMessage, getFacesContext, getFacesMessage, getMessage, getStringValue, getSummaryMessage, getValueBinding, isTransient, restoreAttachedState, restoreState, saveAttachedState, saveState, setDetailMessage, setMessage, setSummaryMessage, setTransient, setValueBinding
 
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 String VALIDATOR_ID

The standard converter id for this converter.

See Also:
Constant Field Values

COMPARE_TO_MESSAGE_ID

public static final 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 String OPERATOR_EQUALS
See Also:
Constant Field Values

OPERATOR_NOT_EQUALS

public static final String OPERATOR_NOT_EQUALS
See Also:
Constant Field Values

OPERATOR_GREATER_THAN

public static final String OPERATOR_GREATER_THAN
See Also:
Constant Field Values

OPERATOR_LESS_THAN

public static final String OPERATOR_LESS_THAN
See Also:
Constant Field Values

OPERATOR_GREATER_THAN_OR_EQUALS

public static final String OPERATOR_GREATER_THAN_OR_EQUALS
See Also:
Constant Field Values

OPERATOR_LESS_THAN_OR_EQUALS

public static final String OPERATOR_LESS_THAN_OR_EQUALS
See Also:
Constant Field Values

OPERATOR_EQUALS_ALT

public static final String OPERATOR_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_NOT_EQUALS_ALT

public static final String OPERATOR_NOT_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_GREATER_THAN_ALT

public static final String OPERATOR_GREATER_THAN_ALT
See Also:
Constant Field Values

OPERATOR_LESS_THAN_ALT

public static final String OPERATOR_LESS_THAN_ALT
See Also:
Constant Field Values

OPERATOR_GREATER_THAN_OR_EQUALS_ALT

public static final String OPERATOR_GREATER_THAN_OR_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_LESS_THAN_OR_EQUALS_ALT

public static final String OPERATOR_LESS_THAN_OR_EQUALS_ALT
See Also:
Constant Field Values

OPERATOR_EQUALS_ALT2

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

AbstractCompareToValidator

public AbstractCompareToValidator()
Method Detail

getOperatorForString

protected String getOperatorForString(String operatorSpecified)

nameForOperator

protected String nameForOperator(String operator)

validateOperatorOnComparisonResult

protected boolean validateOperatorOnComparisonResult(String operator,
                                                     int result)

validate

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

getClassCastExceptionMessage

protected String getClassCastExceptionMessage(String name,
                                              Class clazz,
                                              Object object)

createComparator

protected Comparator createComparator()

getFor

@JSFProperty
public abstract 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(String string)
Parameters:
string - the foreign component_id, on which a value should be validated

getOperator

@JSFProperty
public abstract 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(String operator)

getComparator

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

Returns:

setComparator

public abstract void setComparator(Object comparator)

getAlternateOperatorName

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

Returns:

setAlternateOperatorName

public abstract void setAlternateOperatorName(String alternateOperatorName)


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