Interface Binder.Binding<BEAN,TARGET>

Type Parameters:
BEAN - the bean type
TARGET - the target data type of the binding, matches the field type unless a converter has been set
All Superinterfaces:
Serializable
All Known Implementing Classes:
Binder.BindingImpl
Enclosing class:
Binder<BEAN>

public static interface Binder.Binding<BEAN,TARGET> extends Serializable
Represents the binding between a field and a data property.
See Also:
  • Method Details

    • getField

      HasValue<?,?> getField()
      Gets the field the binding uses.
      Returns:
      the field for the binding
    • validate

      default BindingValidationStatus<TARGET> validate()
      Validates the field value and returns a ValidationStatus instance representing the outcome of the validation. This method is a short-hand for calling validate(boolean) with fireEvent true.
      Returns:
      the validation result.
      See Also:
    • validate

      BindingValidationStatus<TARGET> validate(boolean fireEvent)
      Validates the field value and returns a ValidationStatus instance representing the outcome of the validation.
      Parameters:
      fireEvent - true to fire status event; false to not
      Returns:
      the validation result.
      See Also:
    • getValidationStatusHandler

      BindingValidationStatusHandler getValidationStatusHandler()
      Gets the validation status handler for this Binding.
      Returns:
      the validation status handler for this binding
    • unbind

      void unbind()
      Unbinds the binding from its respective Binder. Removes any ValueChangeListener Registration from associated HasValue.
    • read

      void read(BEAN bean)
      Reads the value from given item and stores it to the bound field.
      Parameters:
      bean - the bean to read from
    • setReadOnly

      void setReadOnly(boolean readOnly)
      Sets the read-only status on for this Binding. Setting a Binding read-only will mark the field read-only and not write any values from the fields to the bean.

      This helper method is the preferred way to control the read-only state of the bound field.

      Parameters:
      readOnly - true to set binding read-only; false to enable writes
      Throws:
      IllegalStateException - if trying to make binding read-write and the setter is null
    • isReadOnly

      boolean isReadOnly()
      Gets the current read-only status for this Binding.
      Returns:
      true if read-only; false if not
      See Also:
    • getGetter

      ValueProvider<BEAN,TARGET> getGetter()
      Gets the getter associated with this Binding.
      Returns:
      the getter
    • getSetter

      Setter<BEAN,TARGET> getSetter()
      Gets the setter associated with this Binding.
      Returns:
      the setter
    • setAsRequiredEnabled

      void setAsRequiredEnabled(boolean asRequiredEnabled)
      Enable or disable asRequired validator. The validator is enabled by default.
      Parameters:
      asRequiredEnabled - false if asRequired validator should be disabled, true otherwise (default)
      See Also:
    • isAsRequiredEnabled

      boolean isAsRequiredEnabled()
      Returns whether asRequired validator is currently enabled or not.
      Returns:
      false if asRequired validator is disabled true otherwise (default)
      See Also:
    • setValidatorsDisabled

      void setValidatorsDisabled(boolean validatorsDisabled)
      Define whether validators are disabled or enabled for this specific binding.
      Parameters:
      validatorsDisabled - A boolean value.
    • isValidatorsDisabled

      boolean isValidatorsDisabled()
      Returns if validators are currently disabled or not.
      Returns:
      A boolean value.
    • setDefaultValidatorEnabled

      void setDefaultValidatorEnabled(Boolean defaultValidatorEnabled)
      Sets up this binding to either enable or disable the default field validator (e.g. min/max validators in DatePicker). This binding-level setting will override the Binder-level setting for this property. Defaults to null.
      Parameters:
      defaultValidatorEnabled - true to enable default validator for this binding, false to disable it, null to reset (fall back to Binder-level setting)
      See Also:
    • isDefaultValidatorEnabled

      Boolean isDefaultValidatorEnabled()
      Returns if default validator of bound field is enabled.
      Returns:
      true if default validator is enabled for this binding, false if it is disabled, null if falls back to Binder-level setting
    • setConvertBackToPresentation

      void setConvertBackToPresentation(boolean convertBackToPresentation)
      Define whether the value should be converted back to the presentation in the field when a converter is used in binding.

      As of version 6.0, when a converter is used on a binding and the user input value is modified by the converter, the value from the converter is applied back to the input. It is possible to control this behavior with this API.

      Parameters:
      convertBackToPresentation - A boolean value
      See Also:
    • isConvertBackToPresentation

      boolean isConvertBackToPresentation()
      Returns whether the value is converted back to the presentation in the field when a converter is used in binding.
      Returns:
      A boolean value
    • hasChanges

      boolean hasChanges()
      Checks whether the field that the binding uses has uncommitted changes.
      Returns:
      true if the field the binding uses has uncommitted changes, otherwise false.
      Throws:
      IllegalStateException - if the binding is no longer attached to a Binder.
    • getEqualityPredicate

      SerializableBiPredicate<TARGET,TARGET> getEqualityPredicate()
      Used in comparison of the current value of a field with its initial value.

      Once set, the value of the field that binding uses will be compared with the initial value for hasChanged.

      Returns:
      the predicate to use for equality comparison
    • isApplied

      default boolean isApplied()
      Checks whether this binding should be processed during validation and writing to bean.
      Returns:
      true if this binding should be processed, false if this binding should be ignored
    • getIsAppliedPredicate

      default SerializablePredicate<Binder.Binding<BEAN,TARGET>> getIsAppliedPredicate()
      Gets predicate for testing isApplied(). By default, non-visible components are ignored during validation and bean writing.
      Returns:
      predicate for testing isApplied()
    • setIsAppliedPredicate

      void setIsAppliedPredicate(SerializablePredicate<Binder.Binding<BEAN,TARGET>> isAppliedPredicate)
      Sets a custom predicate for testing isApplied(). Set to null to restore default functionality.
      Parameters:
      isAppliedPredicate - custom predicate for testing isApplied()
    • value

      default TARGET value()
      Returns the current converted and validated value of this binding's field.

      This method is primarily designed for implementing cross-field validation, where one field's validator needs to access the value of another field. It should be called from within a validator that is registered to a binding via Binder.BindingBuilder.withValidator(Validator).

      The Binder automatically runs validators inside a Signal.effect(Component, com.vaadin.flow.signals.function.EffectAction) context. This makes validators reactive to signal changes - when you call value() on another binding from within a validator, the validator will automatically re-run whenever that other binding's value changes.

      For cross-field validation to work automatically, the fields must be attached to the UI component tree. Detached fields will not trigger automatic re-validation, though manual validation via Binder.validate() will still work.

      Example - Cross-field validation:

       
       Binder<UserRegistration> binder = new Binder<>(
               UserRegistration.class);
       PasswordField passwordField = new PasswordField();
       PasswordField confirmField = new PasswordField();
      
       // Get reference to binding for cross-field validation
       Binding<UserRegistration, String> passwordBinding = binder
               .forField(passwordField).bind("password");
      
       binder.forField(confirmField)
               .withValidator(text -> text.equals(passwordBinding.value()),
                       "Both fields must match")
               .bind("confirmPassword");
      
       add(passwordField, confirmField);
      
       binder.setBean(userRegistration);
      
       passwordField.setValue("secret"); // confirmField shows validation
                                         // error
      
       // Same works also with a Signal directly:
       ValueSignal<String> passwordSignal = new ValueSignal<>("");
       passwordField.bindValue(passwordSignal, passwordSignal::set);
       binder.forField(confirmField)
               .withValidator(text -> text.equals(passwordSignal.get()),
                       "Both fields must match")
               .bind("confirmPassword");
       passwordSignal.set("secret"); // confirmField shows validation
                                     // error
       
       
      Returns:
      the current converted and validated value of this binding's field
      Since:
      25.1
      See Also: