com.vaadin.flow.data.validator.
Class AbstractValidator<T>
- java.lang.Object
-
- com.vaadin.flow.data.validator.AbstractValidator<T>
-
Type Parameters:
T
- The value typeAll Implemented Interfaces:
Validator<T>, Serializable, BiFunction<T,ValueContext,ValidationResult>
Direct Known Subclasses:
public abstract class AbstractValidator<T> extends Object implements Validator<T>
An abstract base class for typed validators.
Since:
1.0.
Author:
Vaadin Ltd
See Also:
-
-
Constructor Summary
Constructors Modifier Constructor and Description protected
AbstractValidator(String errorMessage)
Constructs a validator with the given error message.
-
Method Summary
All Methods Modifier and Type Method and Description protected String
getMessage(T value)
Returns the error message for the given value.
protected ValidationResult
toResult(T value, boolean isValid)
A helper method for creating a
Result
from a value and a validity flag.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.vaadin.flow.data.binder.Validator
alwaysPass, apply, from, from, from, from
-
Methods inherited from interface java.util.function.BiFunction
andThen
-
-
-
-
Constructor Detail
-
AbstractValidator
protected AbstractValidator(String errorMessage)
Constructs a validator with the given error message. The substring "{0}" is replaced by the value that failed validation.
Parameters:
errorMessage
- the message to be included in a failed result, not null
-
-
Method Detail
-
getMessage
protected String getMessage(T value)
Returns the error message for the given value.
Parameters:
value
- an invalid valueReturns:
the formatted error message
-
toResult
protected ValidationResult toResult(T value, boolean isValid)
A helper method for creating a
Result
from a value and a validity flag. If the flag is true, returnsResult.ok
, otherwise yieldsResult.error
bearing the error message returned bygetMessage(Object)
.For instance, the following
apply
method only accepts even numbers:@Override public Result<T> apply(Integer value) { return toResult(value, value % 2 == 0); }
Parameters:
value
- the validated valueisValid
- whether the value is valid or notReturns:
the validation result
-
-