Package com.vaadin.data.validator
Class AbstractValidator<T>
- java.lang.Object
-
- com.vaadin.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
AbstractValidator
implementation that provides a basic Validator implementation except theisValidValue(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 fromvalidate(Object)
.Since Vaadin 7, subclasses can either implement
validate(Object)
directly or implementisValidValue(Object)
when migrating legacy applications. To check validity,validate(Object)
should be used.- Since:
- 5.4
- Author:
- Vaadin Ltd.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.vaadin.data.Validator
Validator.EmptyValueException, Validator.InvalidValueException
-
-
Constructor Summary
Constructors Constructor Description AbstractValidator(String errorMessage)
Constructs a validator with the given error message.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description String
getErrorMessage()
Returns the message to be included in the exception in case the value does not validate.abstract Class<T>
getType()
boolean
isValid(Object value)
Since Vaadin 7, subclasses of AbstractValidator should overrideisValidValue(Object)
orvalidate(Object)
instead ofisValid(Object)
.protected boolean
isValidType(Object value)
Checks the type of the value to validate to ensure it conforms with getType.protected abstract boolean
isValidValue(T value)
Internally check the validity of a value.void
setErrorMessage(String errorMessage)
Sets the message to be included in the exception in case the value does not validate.void
validate(Object value)
Checks the given value against this validator.
-
-
-
Constructor Detail
-
AbstractValidator
public AbstractValidator(String errorMessage)
Constructs a validator with the given error message.- Parameters:
errorMessage
- the message to be included in anValidator.InvalidValueException
(with "{0}" replaced by the value that failed validation).
-
-
Method Detail
-
isValid
public boolean isValid(Object value)
Since Vaadin 7, subclasses of AbstractValidator should overrideisValidValue(Object)
orvalidate(Object)
instead ofisValid(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 overridevalidate(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, anValidator.InvalidValueException
is thrown.- Specified by:
validate
in interfaceValidator
- 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.
-
-