Interface HasValidator<V>

Type Parameters:
V - the value type
All Superinterfaces:
Serializable
All Known Implementing Classes:
AbstractNumberField, BigDecimalField, CheckboxGroup, ComboBox, ComboBoxBase, DatePicker, DateTimePicker, EmailField, IntegerField, MultiSelectComboBox, NumberField, PasswordField, RadioButtonGroup, Select, TextArea, TextField, TextFieldBase, TimePicker

public interface HasValidator<V> extends Serializable
A generic interface for field components and other user interface objects that have a user-editable value that should be validated.
Since:
1.0.
Author:
Vaadin Ltd
  • Method Details

    • getDefaultValidator

      default Validator<V> getDefaultValidator()
      Returns a validator that checks the state of the Value. This should be overridden for components with internal value conversion or validation, e.g. when the user is providing a string that has to be parsed into a date. An invalid input from user will be exposed to a Binder and can be seen as a validation failure.
      Returns:
      state validator
    • addValidationStatusChangeListener

      default Registration addValidationStatusChangeListener(ValidationStatusChangeListener<V> listener)
      Enables the implementing components to notify changes in their validation status to the observers.

      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));
               }
           }
       }
       
      Returns:
      Registration of the added listener.
      Since:
      23.2
      See Also: