org.apache.myfaces.custom.comparetovalidator
Class CompareToValidator
java.lang.Object
org.apache.myfaces.validator.ValidatorBase
org.apache.myfaces.custom.comparetovalidator.CompareToValidator
- All Implemented Interfaces:
- java.util.EventListener, javax.faces.component.StateHolder, javax.faces.validator.Validator
public class CompareToValidator
- extends ValidatorBase
- Version:
- $Revision: 472727 $ $Date: 2006-11-09 02:08:46 +0100 (Do, 09 Nov 2006) $
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.
faces-config.xml configuration:
CompareTo validator
org.apache.myfaces.validator.CompareTo
org.apache.myfaces.custom.comparetovalidator.CompareToValidator
Facelets configuration (inside a taglib.xml file):
compareToValidator
org.apache.myfaces.validator.CompareTo
Example usage:
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.
- Author:
- Mike Kienenberger (latest modification by $Author: grantsmith $)
Fields inherited from interface javax.faces.validator.Validator |
NOT_IN_RANGE_MESSAGE_ID |
Methods inherited from class org.apache.myfaces.validator.ValidatorBase |
getDetailMessage, getFacesContext, getFacesMessage, getMessage, getSummaryMessage, getValueBinding, isTransient, restoreAttachedState, saveAttachedState, setDetailMessage, setMessage, setSummaryMessage, setTransient, setValueBinding |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
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
_foreignComponentName
protected java.lang.String _foreignComponentName
_operator
protected java.lang.String _operator
_comparator
protected java.lang.Object _comparator
_alternateOperatorName
protected java.lang.String _alternateOperatorName
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
CompareToValidator
public CompareToValidator()
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 java.lang.String getFor()
- Returns:
- the foreign component_id, on which a value should be validated
setFor
public void setFor(java.lang.String string)
- Parameters:
string
- the foreign component_id, on which a value should be validated
getOperator
public java.lang.String getOperator()
setOperator
public void setOperator(java.lang.String operator)
getComparator
public java.lang.Object getComparator()
setComparator
public void setComparator(java.lang.Object comparator)
getAlternateOperatorName
public java.lang.String getAlternateOperatorName()
setAlternateOperatorName
public void setAlternateOperatorName(java.lang.String alternateOperatorName)
saveState
public java.lang.Object saveState(javax.faces.context.FacesContext context)
- Specified by:
saveState
in interface javax.faces.component.StateHolder
- Overrides:
saveState
in class ValidatorBase
restoreState
public void restoreState(javax.faces.context.FacesContext context,
java.lang.Object state)
- Specified by:
restoreState
in interface javax.faces.component.StateHolder
- Overrides:
restoreState
in class ValidatorBase
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)
Copyright © 2011. All Rights Reserved.