org.apache.wicket.validation.validator
Class AbstractValidator

java.lang.Object
  extended by org.apache.wicket.validation.validator.AbstractValidator
All Implemented Interfaces:
java.io.Serializable, IClusterable, INullAcceptingValidator, IValidator
Direct Known Subclasses:
CreditCardValidator, DateValidator, NumberValidator, StringValidator, UrlValidator

public abstract class AbstractValidator
extends java.lang.Object
implements INullAcceptingValidator, IClusterable

Convenience base class for IValidators. This class is thread-safe and therefore it is safe to share validators across sessions/threads.

Error messages can be registered by calling one of the error (IValidatable) overloads. By default this class will skip validation if the IValidatable.getValue() returns null. Validators that wish to validate the null value need to override validateOnNullValue() and return true.

Since:
1.2.6
Author:
Jonathan Locke, Eelco Hillenius, Igor Vaynberg (ivaynberg)
See Also:
Serialized Form

Constructor Summary
AbstractValidator()
           
 
Method Summary
 void error(IValidatable validatable)
          Reports an error against an IValidatable instance using the Map returned by variablesMap(IValidatable) for variable interpolations and the message resource key returned by resourceKey().
 void error(IValidatable validatable, java.util.Map vars)
          Reports an error against an IValidatable instance using the given Map for variable interpolations and message resource key provided by resourceKey().
 void error(IValidatable validatable, java.lang.String resourceKey)
          Reports an error against an IValidatable instance using the Map returned by variablesMap(IValidatable) for variable interpolations and the given message resource key.
 void error(IValidatable validatable, java.lang.String resourceKey, java.util.Map vars)
          Reports an error against an IValidatable instance using the given message resource key and Map for variable interpolations.
protected  java.util.Map messageModel(FormComponent formComponent)
          Deprecated. use variablesMap(IValidatable) instead
protected abstract  void onValidate(IValidatable validatable)
          Validates the IValidatable instance.
protected  java.lang.String resourceKey()
          Gets the message resource key for this validator's error message from the ApplicationSettings class.
protected  java.lang.String resourceKey(FormComponent formComponent)
          Deprecated. use resourceKey() instead
 void validate(FormComponent component)
          Deprecated. use variablesMap(IValidatable) instead
 void validate(IValidatable validatable)
          Validates the IValidatable instance.
 boolean validateOnNullValue()
          Indicates whether or not to validate the value if it is null.
protected  java.util.Map variablesMap(IValidatable validatable)
          Gets the default Map of variables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractValidator

public AbstractValidator()
Method Detail

validateOnNullValue

public boolean validateOnNullValue()
Indicates whether or not to validate the value if it is null. It is usually desirable to skip validation if the value is null, unless we want to make sure the value is in fact null (a rare use case). Validators that extend this and wish to ensure the value is null should override this method and return true.

Returns:
true to validate on null value, false to skip validation on null value

onValidate

protected abstract void onValidate(IValidatable validatable)
Validates the IValidatable instance.

Parameters:
validatable - the given IValidatable instance

validate

public final void validate(IValidatable validatable)
Description copied from interface: IValidator
Validates the IValidatable instance. Validation errors should be reported using the IValidatable.error(IValidationError) method.

Specified by:
validate in interface IValidator
Parameters:
validatable - the IValidatable instance being validated
See Also:
IValidator.validate(IValidatable)

error

public void error(IValidatable validatable)
Reports an error against an IValidatable instance using the Map returned by variablesMap(IValidatable) for variable interpolations and the message resource key returned by resourceKey().

Parameters:
validatable - the IValidatable instance being validated

error

public void error(IValidatable validatable,
                  java.lang.String resourceKey)
Reports an error against an IValidatable instance using the Map returned by variablesMap(IValidatable) for variable interpolations and the given message resource key.

Parameters:
validatable - the IValidatable instance being validated
resourceKey - the message resource key to use

error

public void error(IValidatable validatable,
                  java.util.Map vars)
Reports an error against an IValidatable instance using the given Map for variable interpolations and message resource key provided by resourceKey().

Parameters:
validatable - the IValidatable instance being validated
vars - Map of variables for variable interpolation

error

public void error(IValidatable validatable,
                  java.lang.String resourceKey,
                  java.util.Map vars)
Reports an error against an IValidatable instance using the given message resource key and Map for variable interpolations.

Parameters:
validatable - the IValidatable instance being validated
resourceKey - the message resource key to use
vars - Map of variables for variable interpolation

resourceKey

protected java.lang.String resourceKey()
Gets the message resource key for this validator's error message from the ApplicationSettings class. NOTE: THIS METHOD SHOULD NEVER RETURN null.

Returns:
the message resource key for this validator

variablesMap

protected java.util.Map variablesMap(IValidatable validatable)
Gets the default Map of variables. NOTE: THIS METHOD SHOULD NEVER RETURN null.

Parameters:
validatable - the IValidatable instance being validated
Returns:
a Map of variables for variable interpolation

messageModel

protected final java.util.Map messageModel(FormComponent formComponent)
Deprecated. use variablesMap(IValidatable) instead

DEPRECATED/UNSUPPORTED Gets the default variables for interpolation. These are:

Parameters:
formComponent - form component
Returns:
a map with the variables for interpolation
Throws:
java.lang.UnsupportedOperationException - FIXME 2.0: remove asap

resourceKey

protected final java.lang.String resourceKey(FormComponent formComponent)
Deprecated. use resourceKey() instead

DEPRECATED/UNSUPPORTED Gets the resource key for validator's error message from the ApplicationSettings class.

Parameters:
formComponent - form component that is being validated
Returns:
the resource key based on the form component
Throws:
java.lang.UnsupportedOperationException - FIXME 2.0: remove asap

validate

public final void validate(FormComponent component)
Deprecated. use variablesMap(IValidatable) instead

DEPRECATED/UNSUPPORTED THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT IMPLEMENT IT.

Instead of subclassing IValidator, you should use one of the existing validators, which cover a huge number of cases, or if none satisfies your need, subclass AbstractValidator.

Validates the given input. The input corresponds to the input from the request for a component.

Parameters:
component - Component to validate
Throws:
java.lang.UnsupportedOperationException - FIXME 2.0: remove asap


Copyright © 2004-2010 Apache Software Foundation. All Rights Reserved.