com.vaadin.data.validator.
Class AbstractValidator<T>
- java.lang.Object
-
- com.vaadin.data.validator.AbstractValidator<T>
-
Type Parameters:
T
- The typeAll Implemented Interfaces:
Direct Known Subclasses:
public abstract class AbstractValidator<T> extends Object implements Validator
Abstract
Validator
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
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:
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.vaadin.data.Validator
Validator.EmptyValueException, Validator.InvalidValueException
-
-
Constructor Summary
Constructors Constructor and Description AbstractValidator(String errorMessage)
Constructs a validator with the given error message.
-
Method Summary
All Methods Modifier and Type Method and 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 override
isValidValue(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 anInvalidValueException
(with "{0}" replaced by the value that failed validation).
-
-
Method Detail
-
isValid
public boolean isValid(Object value)
Since Vaadin 7, subclasses of AbstractValidator should override
isValidValue(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 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 interfaceValidator
Parameters:
value
- the value to checkThrows:
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 checkReturns:
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.
-
-