V
- the value typepublic interface HasValidator<V> extends Serializable
Modifier and Type | Method and Description |
---|---|
default Registration |
addValidationStatusChangeListener(ValidationStatusChangeListener<V> listener)
Enables the implementing components to notify changes in their validation
status to the observers.
|
default Validator<V> |
getDefaultValidator()
Returns a validator that checks the state of the Value.
|
default Validator<V> getDefaultValidator()
Binder
and
can be seen as a validation failure.default Registration addValidationStatusChangeListener(ValidationStatusChangeListener<V> listener)
Note: This method can be overridden by the implementing
classes e.g. components, to enable the associated Binder.Binding
instance subscribing for their validation change events and revalidate
itself.
This method primarily designed for notifying the Binding about the
validation status changes of a bound component at the client-side.
WebComponents such as <vaadin-date-picker>
or any
other component that accept a formatted text as input should be able to
communicate their invalid status to their server-side instance, and a
bound server-side component instance must notify its binding about this
validation status change as well. When the binding instance revalidates,
a chain of validators and convertors get executed one of which is the
default validator provided by getDefaultValidator()
.
Thus, In order for the binding to be able to show/clear errors for its
associated bound field, it is important that implementing components take
that validation status into account while implementing any validator and
converter including getDefaultValidator()
. Here is
an example:
@Tag("date-picker-demo")
public class DatePickerDemo implements HasValidator<LocalDate> {
// Each web component has a way to communicate its validation status
// to its server-side component instance. The following clientSideValid
// state is introduced here just for the sake of simplicity of this code
// snippet:
boolean clientSideValid = true;
/**
* Note how clientSideValid
engaged in the definition
* of this method. It is important to reflect this status either
* in the returning validation result of this method or any other
* validation that is associated with this component.
*/
@Override
public Validator getDefaultValidator() {
return (value, valueContext) -> clientSideValid ? ValidationResult.ok()
: ValidationResult.error("Invalid date format");
}
private final Collection<ValidationStatusChangeListener<LocalDate>>
validationStatusListeners = new ArrayList<>();
/**
* This enables the binding to subscribe for the validation status
* change events that are fired by this component and revalidate
* itself respectively.
*/
@Override
public Registration addValidationStatusChangeListener(
ValidationStatusChangeListener<LocalDate> listener) {
validationStatusListeners.add(listener);
return () -> validationStatusListeners.remove(listener);
}
private void fireValidationStatusChangeEvent(
boolean newValidationStatus) {
if (this.clientSideValid != newValidationStatus) {
this.clientSideValid = newValidationStatus;
var event = new ValidationStatusChangeEvent<>(this,
newValidationStatus);
validationStatusListeners.forEach(
listener -> listener.validationStatusChanged(event));
}
}
}
Binder.BindingBuilderImpl.bind(ValueProvider,
Setter)
Copyright © 2024. All rights reserved.