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
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.vaadin.featurepack.data.Validator
Validator.EmptyValueException, Validator.InvalidValueException -
Constructor Summary
ConstructorsConstructorDescriptionAbstractValidator(String errorMessage) Constructs a validator with the given error message. -
Method Summary
Modifier and TypeMethodDescriptionReturns the message to be included in the exception in case the value does not validate.getType()booleanSince Vaadin 7, subclasses of AbstractValidator should overrideisValidValue(Object)orvalidate(Object)instead ofisValid(Object).protected booleanisValidType(Object value) Checks the type of the value to validate to ensure it conforms with getType.protected abstract booleanisValidValue(T value) Internally check the validity of a value.voidsetErrorMessage(String errorMessage) Sets the message to be included in the exception in case the value does not validate.voidChecks the given value against this validator.
-
Constructor Details
-
AbstractValidator
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 Details
-
isValid
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
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
Description copied from interface:ValidatorChecks the given value against this validator. If the value is valid the method does nothing. If the value is invalid, anValidator.InvalidValueExceptionis thrown.- Specified by:
validatein interfaceValidator- Parameters:
value- the value to check- Throws:
Validator.InvalidValueException- if the value is invalid
-
isValidType
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
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
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
-