Interface HasValidation

All Superinterfaces:
Serializable
All Known Subinterfaces:
HasValidationProperties
All Known Implementing Classes:
AbstractNumberField, BigDecimalField, Checkbox, CheckboxGroup, ComboBox, ComboBoxBase, CustomField, DatePicker, DateTimePicker, EmailField, IntegerField, MultiSelectComboBox, NumberField, PasswordField, RadioButtonGroup, RangeSlider, Select, Slider, TextArea, TextField, TextFieldBase, TimePicker

public interface HasValidation extends Serializable
A component that supports input validation.

HasValidation is implemented by component when used with a Binder and input is validated with a Binder.

Since:
1.0.
Author:
Vaadin Ltd
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    Binds the component's error message to the provided signal so that the error message is kept in sync with the signal's current value.
    default void
    Binds the component's invalid state to the provided signal so that the invalid flag is kept in sync with the signal's current value.
    Gets the current error message from the component.
    boolean
    Returns true if component input is invalid, false otherwise.
    void
    setErrorMessage(String errorMessage)
    Sets an error message to the component.
    void
    setInvalid(boolean invalid)
    Sets the validity of the component input.
    default void
    setManualValidation(boolean enabled)
    Sets whether manual validation mode is enabled for the component.
  • Method Details

    • setManualValidation

      default void setManualValidation(boolean enabled)
      Sets whether manual validation mode is enabled for the component.

      When enabled, the component doesn't perform its built-in constraint validation on value change, blur, and other events. This allows manually controlling the invalid state and error messages using the setInvalid(boolean) and setErrorMessage(String) methods. Manual mode is helpful when there is a need for a totally custom validation logic that cannot be achieved with Binder.

      Example:

       Field field = new Field();
       field.setManualValidation(true);
       field.addValueChangeListener(event -> {
           if (Objects.equal(event.getValue(), "")) {
               field.setInvalid(true);
               field.setErrorMessage("The field is required.");
           } else {
               field.setInvalid(false);
           }
       });
       

      For components that don't have built-in validation, the method has no effect.

      Parameters:
      enabled - whether to enable manual validation mode.
    • setErrorMessage

      void setErrorMessage(String errorMessage)
      Sets an error message to the component.

      The Web Component is responsible for deciding when to show the error message to the user, and this is usually triggered by triggering the invalid state for the Web Component. Which means that there is no need to clean up the message when component becomes valid (otherwise it may lead to undesired visual effects).

      While a signal binding for the error message is active, calls to this method throw a com.vaadin.flow.signals.BindingActiveException.

      Parameters:
      errorMessage - a new error message
      Throws:
      BindingActiveException - thrown when there is already an existing signal binding
    • getErrorMessage

      String getErrorMessage()
      Gets the current error message from the component.
      Returns:
      current error message
    • bindErrorMessage

      default void bindErrorMessage(Signal<String> signal)
      Binds the component's error message to the provided signal so that the error message is kept in sync with the signal's current value.

      Passing null as the signal removes any existing binding. When unbinding, the current error message is left unchanged.

      While a binding is active, manual calls to setErrorMessage(String) throw a com.vaadin.flow.signals.BindingActiveException. Bindings are lifecycle-aware and only active while the owning Component is in attached state; they are deactivated while the component is in detached state.

      Parameters:
      signal - the signal providing error messages, not null
      Throws:
      BindingActiveException - thrown when there is already an existing binding
      Since:
      25.1
    • setInvalid

      void setInvalid(boolean invalid)
      Sets the validity of the component input.

      When component becomes valid it hides the error message by itself, so there is no need to clean up the error message via the setErrorMessage(String) call.

      NOTE: If you need to manually control the invalid state, consider enabling manual validation mode with setManualValidation(boolean) to avoid potential conflicts between your custom validation and the component's built-in validation.

      While a signal binding for the invalid state is active, calls to this method throw a com.vaadin.flow.signals.BindingActiveException.

      Parameters:
      invalid - new value for component input validity
      Throws:
      BindingActiveException - thrown when there is already an existing signal binding
    • isInvalid

      boolean isInvalid()
      Returns true if component input is invalid, false otherwise.
      Returns:
      whether the component input is valid
    • bindInvalid

      default void bindInvalid(Signal<Boolean> signal)
      Binds the component's invalid state to the provided signal so that the invalid flag is kept in sync with the signal's current value.

      Passing null as the signal removes any existing binding. When unbinding, the current invalid state is left unchanged.

      While a binding is active, manual calls to setInvalid(boolean) throw a com.vaadin.flow.signals.BindingActiveException. Bindings are lifecycle-aware and only active while the owning component is in the attached state; they are deactivated while the component is in the detached state.

      Parameters:
      signal - the signal providing invalid state flags, or null to unbind
      Throws:
      BindingActiveException - thrown when there is already an existing binding
      Since:
      25.1