Class BeanValidationBinder<BEAN>

java.lang.Object
com.vaadin.flow.data.binder.Binder<BEAN>
com.vaadin.flow.data.binder.BeanValidationBinder<BEAN>
Type Parameters:
BEAN - the bean type
All Implemented Interfaces:
Serializable

public class BeanValidationBinder<BEAN> extends Binder<BEAN>
Binder that uses reflection based on the provided bean type to resolve bean properties. The Binder automatically adds BeanValidator which validates beans using JSR-303 specification. It assumes that JSR-303 bean validation implementation is present on the classpath.

By default only the constraints of the default validation group are validated. Use setValidationGroups(Class...) to configure the validation groups to use instead, and validate(Class<?>...) or isValid(Class<?>...) to run a single validation against other validation groups without changing the configured ones.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • BeanValidationBinder

      public BeanValidationBinder(Class<BEAN> beanType)
      Creates a new binder that uses reflection based on the provided bean type to resolve bean properties. It assumes that JSR-303 bean validation implementation is present on the classpath. If there is no such implementation available then Binder class should be used instead (this constructor will throw an exception). Otherwise BeanValidator is added to each binding that is defined using a property name.
      Parameters:
      beanType - the bean type to use, not null
    • BeanValidationBinder

      public BeanValidationBinder(Class<BEAN> beanType, boolean scanNestedDefinitions)
      Creates a new binder that uses reflection based on the provided bean type to resolve bean properties. It assumes that JSR-303 bean validation implementation is present on the classpath. If there is no such implementation available then Binder class should be used instead (this constructor will throw an exception). Otherwise BeanValidator is added to each binding that is defined using a property name.
      Parameters:
      beanType - the bean type to use, not null
      scanNestedDefinitions - if true, scan for nested property definitions as well
      Since:
      2.2
    • BeanValidationBinder

      public BeanValidationBinder(Class<BEAN> beanType, Class<?>... validationGroups)
      Creates a new binder that uses reflection based on the provided bean type to resolve bean properties, and validates the constraints of the given validation groups instead of the ones of the default group.

      See setValidationGroups(Class...) for details on how the validation groups are used.

      Parameters:
      beanType - the bean type to use, not null
      validationGroups - the validation groups to validate against, or none to use the default group
      Since:
      25.3
    • BeanValidationBinder

      public BeanValidationBinder(Class<BEAN> beanType, boolean scanNestedDefinitions, Class<?>... validationGroups)
      Creates a new binder that uses reflection based on the provided bean type to resolve bean properties, and validates the constraints of the given validation groups instead of the ones of the default group.

      See setValidationGroups(Class...) for details on how the validation groups are used.

      Parameters:
      beanType - the bean type to use, not null
      scanNestedDefinitions - if true, scan for nested property definitions as well
      validationGroups - the validation groups to validate against, or none to use the default group
      Since:
      25.3
  • Method Details

    • setValidationGroups

      public void setValidationGroups(Class<?>... validationGroups)
      Sets the validation groups whose constraints are validated by this binder. Configuring the validation groups affects all validation triggered by this binder, including the validation of a single field when its value changes.

      Note that the validation groups replace, rather than extend, the default group: pass Default.class explicitly to validate the constraints that do not declare a group as well. Passing no groups at all restores the default behavior of validating only the default group.

      The required indicators of the already bound fields are updated to match the new validation groups, unless the indicator has been changed by the application after the field was bound. Validation results that are already shown are not updated, i.e. the application should call Binder.validate() after changing the validation groups if the fields have already been validated against the previous groups.

      Parameters:
      validationGroups - the validation groups to validate against, or none to use the default group
      Throws:
      IllegalArgumentException - if any of the given validation groups is not an interface
      Since:
      25.3
      See Also:
    • getValidationGroups

      public Class<?>[] getValidationGroups()
      Gets the validation groups whose constraints are validated by this binder. An empty array means that the default group is used.

      While a validation triggered by validate(Class...) or isValid(Class...) is running, the groups given to that method are returned instead of the configured ones. Bean level validators added with Binder.withValidator(Validator) can use this to validate against the same groups as the field level validation.

      Returns:
      the validation groups in effect, not null
      Since:
      25.3
      See Also:
    • validate

      public BinderValidationStatus<BEAN> validate(Class<?>... validationGroups)
      Validates the values of all bound fields against the constraints of the given validation groups and returns the validation status. The configured validation groups are left unchanged, i.e. any validation triggered later on, for instance by a field value change, uses the configured groups again.

      This can be used to run constraints that should not be validated while the user is editing, such as constraints that are only relevant when the data is saved:

       saveButton.addClickListener(event -> {
           if (binder.validate(Save.class).isOk()) {
               binder.writeBean(bean);
           }
       });
       

      Calling this method without any validation groups is equivalent to calling Binder.validate(). Use isValid(Class...) to validate without showing the validation results to the user.

      Parameters:
      validationGroups - the validation groups to validate against
      Returns:
      validation status for the binder
      Throws:
      IllegalArgumentException - if any of the given validation groups is not an interface
      Since:
      25.3
      See Also:
    • isValid

      public boolean isValid(Class<?>... validationGroups)
      Runs all currently configured field level validators, as well as all bean level validators if a bean is currently set with Binder.setBean(Object), against the constraints of the given validation groups, and returns whether any of the validators failed. The configured validation groups are left unchanged.

      Unlike validate(Class...), this method does not trigger status change events and does not modify the UI, which makes it suitable for example for enabling and disabling a save button.

      Calling this method without any validation groups is equivalent to calling Binder.isValid().

      Parameters:
      validationGroups - the validation groups to validate against
      Returns:
      whether this binder is in a valid state
      Throws:
      IllegalArgumentException - if any of the given validation groups is not an interface
      Since:
      25.3
      See Also:
    • validate

      protected BinderValidationStatus<BEAN> validate(boolean fireEvent, Class<?>... validationGroups)
      Validates the values of all bound fields against the constraints of the given validation groups and returns the validation status. This method can skip firing the event, based on the given boolean.
      Parameters:
      fireEvent - true to fire validation status events; false to not
      validationGroups - the validation groups to validate against
      Returns:
      validation status for the binder
      Throws:
      IllegalArgumentException - if any of the given validation groups is not an interface
      Since:
      25.3
      See Also:
    • setRequiredConfigurator

      public void setRequiredConfigurator(RequiredFieldConfigurator configurator)
      Sets a logic which allows to configure require indicator via HasValue.setRequiredIndicatorVisible(boolean) based on property descriptor.

      Required indicator configuration will not be used at all if configurator is null.

      By default the RequiredFieldConfigurator.DEFAULT configurator is used.

      Parameters:
      configurator - required indicator configurator, may be null
    • getRequiredConfigurator

      public RequiredFieldConfigurator getRequiredConfigurator()
      Gets field required indicator configuration logic.
      Returns:
      required indicator configurator, may be null
      See Also:
    • configureBinding

      protected Binder.BindingBuilder<BEAN,?> configureBinding(Binder.BindingBuilder<BEAN,?> binding, PropertyDefinition<BEAN,?> definition)
      Description copied from class: Binder
      Configures the binding with the property definition definition before it's being bound.
      Overrides:
      configureBinding in class Binder<BEAN>
      Parameters:
      binding - a binding to configure
      definition - a property definition information
      Returns:
      the new configured binding
    • removeBindingInternal

      protected void removeBindingInternal(Binder.Binding<BEAN,?> binding)
      Description copied from class: Binder
      Removes (internally) the Binding from the bound properties map (if present) and from the list of Bindings. Note that this DOES NOT remove the ValueChangeListener that the Binding might have registered with any HasValues or decouple the Binder from within the Binding. To do that, use Binder.Binding.unbind() This method should just be used for internal cleanup.
      Overrides:
      removeBindingInternal in class Binder<BEAN>
      Parameters:
      binding - The Binding to remove from the binding map