org.apache.wicket.validation.validator
Class AbstractValidator<T>

java.lang.Object
  extended by org.apache.wicket.validation.validator.AbstractValidator<T>
Type Parameters:
T - type of validatable
All Implemented Interfaces:
Serializable, IClusterable, INullAcceptingValidator<T>, IValidator<T>
Direct Known Subclasses:
CreditCardValidator, DateValidator, NumberValidator, StringValidator, UrlValidator

public abstract class AbstractValidator<T>
extends Object
implements INullAcceptingValidator<T>, 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<T> 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<T> validatable, Map<String,Object> 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<T> validatable, 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<T> validatable, String resourceKey, Map<String,Object> vars)
          Reports an error against an IValidatable instance using the given message resource key and Map for variable interpolations.
protected  Map<String,Object> messageModel(FormComponent<T> formComponent)
          Deprecated. use variablesMap(IValidatable) instead
protected abstract  void onValidate(IValidatable<T> validatable)
          Validates the IValidatable instance.
protected  String resourceKey()
          Gets the message resource key for this validator's error message from the ApplicationSettings class.
protected  String resourceKey(FormComponent<T> formComponent)
          Deprecated. use resourceKey() instead
 void validate(FormComponent<T> component)
          Deprecated. use variablesMap(IValidatable) instead
 void validate(IValidatable<T> validatable)
          Validates the IValidatable instance.
 boolean validateOnNullValue()
          Indicates whether or not to validate the value if it is null.
protected  Map<String,Object> variablesMap(IValidatable<T> 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<T> validatable)
Validates the IValidatable instance.

Parameters:
validatable - the given IValidatable instance

validate

public final void validate(IValidatable<T> 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<T>
Parameters:
validatable - the IValidatable instance being validated
See Also:
IValidator.validate(IValidatable)

error

public void error(IValidatable<T> 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<T> validatable,
                  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<T> validatable,
                  Map<String,Object> 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<T> validatable,
                  String resourceKey,
                  Map<String,Object> 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 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 Map<String,Object> variablesMap(IValidatable<T> 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

@Deprecated
protected final Map<String,Object> messageModel(FormComponent<T> 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:
UnsupportedOperationException - FIXME 2.0: remove asap

resourceKey

@Deprecated
protected final String resourceKey(FormComponent<T> 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:
UnsupportedOperationException - FIXME 2.0: remove asap

validate

@Deprecated
public final void validate(FormComponent<T> 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:
UnsupportedOperationException - FIXME 2.0: remove asap


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