Class AbstractValidator<T>

java.lang.Object
com.vaadin.featurepack.data.validator.AbstractValidator<T>
Type Parameters:
T - The type
All Implemented Interfaces:
Validator, Serializable
Direct Known Subclasses:
AbstractStringValidator, RangeValidator

public abstract class AbstractValidator<T> extends Object implements Validator
Abstract Validator implementation that provides a basic Validator implementation except the isValidValue(Object) method.

To include the value that failed validation in the exception message you can use "{0}" in the error message. This will be replaced with the failed value (converted to string using Object.toString()) or "null" if the value is null.

The default implementation of AbstractValidator does not support HTML in error messages. To enable HTML support, override Validator.InvalidValueException.getHtmlMessage() and throw such exceptions from validate(Object).

Since Vaadin 7, subclasses can either implement validate(Object) directly or implement isValidValue(Object) when migrating legacy applications. To check validity, validate(Object) should be used.

Author:
Vaadin Ltd.
See Also:
  • Constructor Details

    • AbstractValidator

      public AbstractValidator(String errorMessage)
      Constructs a validator with the given error message.
      Parameters:
      errorMessage - the message to be included in an Validator.InvalidValueException (with "{0}" replaced by the value that failed validation).
  • Method Details

    • isValid

      public boolean isValid(Object value)
      Since Vaadin 7, subclasses of AbstractValidator should override isValidValue(Object) or validate(Object) instead of isValid(Object). validate(Object) should normally be used to check values.
      Parameters:
      value -
      Returns:
      true if the value is valid
    • isValidValue

      protected abstract boolean isValidValue(T value)
      Internally check the validity of a value. This method can be used to perform validation in subclasses if customization of the error message is not needed. Otherwise, subclasses should override validate(Object) and the return value of this method is ignored. This method should not be called from outside the validator class itself.
      Parameters:
      value -
      Returns:
    • validate

      public void validate(Object value) throws Validator.InvalidValueException
      Description copied from interface: Validator
      Checks the given value against this validator. If the value is valid the method does nothing. If the value is invalid, an Validator.InvalidValueException is thrown.
      Specified by:
      validate in interface Validator
      Parameters:
      value - the value to check
      Throws:
      Validator.InvalidValueException - if the value is invalid
    • isValidType

      protected boolean isValidType(Object value)
      Checks the type of the value to validate to ensure it conforms with getType. Enables sub classes to handle the specific type instead of Object.
      Parameters:
      value - The value to check
      Returns:
      true if the value can safely be cast to the type specified by getType()
    • getErrorMessage

      public String getErrorMessage()
      Returns the message to be included in the exception in case the value does not validate.
      Returns:
      the error message provided in the constructor or using setErrorMessage(String).
    • setErrorMessage

      public void setErrorMessage(String errorMessage)
      Sets the message to be included in the exception in case the value does not validate. The exception message is typically shown to the end user.
      Parameters:
      errorMessage - the error message. "{0}" is automatically replaced by the value that did not validate.
    • getType

      public abstract Class<T> getType()