com.vaadin.data.validator.

Class 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 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.

    Since:

    5.4

    Author:

    Vaadin Ltd.

    See Also:

    Serialized Form

    • Constructor Detail

      • AbstractValidator

        public AbstractValidator(String errorMessage)

        Constructs a validator with the given error message.

        Parameters:

        errorMessage - the message to be included in an InvalidValueException (with "{0}" replaced by the value that failed validation).

    • Method Detail

      • 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:

      • 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()