Class BeanValidationBinder<BEAN>
- Type Parameters:
BEAN- the bean type
- All Implemented Interfaces:
Serializable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.data.binder.Binder
Binder.Binding<BEAN,TARGET>, Binder.BindingBuilder<BEAN, TARGET>, Binder.BindingBuilderImpl<BEAN, FIELDVALUE, TARGET>, Binder.BindingImpl<BEAN, FIELDVALUE, TARGET> -
Constructor Summary
ConstructorsConstructorDescriptionBeanValidationBinder(Class<BEAN> beanType) Creates a new binder that uses reflection based on the provided bean type to resolve bean properties.BeanValidationBinder(Class<BEAN> beanType, boolean scanNestedDefinitions) Creates a new binder that uses reflection based on the provided bean type to resolve bean properties.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.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. -
Method Summary
Modifier and TypeMethodDescriptionprotected Binder.BindingBuilder<BEAN, ?> configureBinding(Binder.BindingBuilder<BEAN, ?> binding, PropertyDefinition<BEAN, ?> definition) Configures thebindingwith the property definitiondefinitionbefore it's being bound.Gets field required indicator configuration logic.Class<?>[]Gets the validation groups whose constraints are validated by this binder.booleanRuns all currently configured field level validators, as well as all bean level validators if a bean is currently set withBinder.setBean(Object), against the constraints of the given validation groups, and returns whether any of the validators failed.protected voidremoveBindingInternal(Binder.Binding<BEAN, ?> binding) Removes (internally) theBindingfrom the bound properties map (if present) and from the list ofBindings.voidsetRequiredConfigurator(RequiredFieldConfigurator configurator) Sets a logic which allows to configure require indicator viaHasValue.setRequiredIndicatorVisible(boolean)based on property descriptor.voidsetValidationGroups(Class<?>... validationGroups) Sets the validation groups whose constraints are validated by this binder.protected BinderValidationStatus<BEAN> Validates the values of all bound fields against the constraints of the given validation groups and returns the validation status.Validates the values of all bound fields against the constraints of the given validation groups and returns the validation status.Methods inherited from class com.vaadin.flow.data.binder.Binder
addListener, addStatusChangeListener, addValueChangeListener, bind, bind, bindInstanceFields, bindReadOnly, bindReadOnly, clearError, createBinding, doCreateBinding, forField, forMemberField, getBean, getBeanState, getBinding, getBindingExceptionHandler, getBindings, getChangedBindings, getConverterFactory, getFields, getStatusLabel, getValidationErrorHandler, getValidationStatusHandler, handleBinderValidationStatus, handleError, handleFieldValueChange, handleValidationStatus, hasChanges, hasChanges, isApplyBindingsToHiddenFields, isChangeDetectionEnabled, isDefaultValidatorsEnabled, isFieldsValidationStatusChangeListenerEnabled, isValid, isValidatorsDisabled, readBean, readRecord, refreshFields, removeBean, removeBinding, removeBinding, removeBinding, restoreBeanState, setApplyBindingsToHiddenFields, setBean, setBindingExceptionHandler, setChangeDetectionEnabled, setDefaultValidatorsEnabled, setFieldsValidationStatusChangeListenerEnabled, setReadOnly, setStatusLabel, setValidationErrorHandler, setValidationStatusHandler, setValidatorsDisabled, validate, validate, validationStatusSignal, withPropertySet, withValidator, withValidator, withValidator, writeBean, writeBean, writeBeanAsDraft, writeBeanAsDraft, writeBeanIfValid, writeChangedBindingsToBean, writeRecord
-
Constructor Details
-
BeanValidationBinder
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 thenBinderclass should be used instead (this constructor will throw an exception). OtherwiseBeanValidatoris added to each binding that is defined using a property name.- Parameters:
beanType- the bean type to use, notnull
-
BeanValidationBinder
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 thenBinderclass should be used instead (this constructor will throw an exception). OtherwiseBeanValidatoris added to each binding that is defined using a property name.- Parameters:
beanType- the bean type to use, notnullscanNestedDefinitions- iftrue, scan for nested property definitions as well- Since:
- 2.2
-
BeanValidationBinder
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, notnullvalidationGroups- 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, notnullscanNestedDefinitions- iftrue, scan for nested property definitions as wellvalidationGroups- the validation groups to validate against, or none to use the default group- Since:
- 25.3
-
-
Method Details
-
setValidationGroups
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.classexplicitly 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
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...)orisValid(Class...)is running, the groups given to that method are returned instead of the configured ones. Bean level validators added withBinder.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
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(). UseisValid(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
Runs all currently configured field level validators, as well as all bean level validators if a bean is currently set withBinder.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
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 givenboolean.- Parameters:
fireEvent-trueto fire validation status events;falseto notvalidationGroups- 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
Sets a logic which allows to configure require indicator viaHasValue.setRequiredIndicatorVisible(boolean)based on property descriptor.Required indicator configuration will not be used at all if
configuratoris null.By default the
RequiredFieldConfigurator.DEFAULTconfigurator is used.- Parameters:
configurator- required indicator configurator, may benull
-
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:BinderConfigures thebindingwith the property definitiondefinitionbefore it's being bound.- Overrides:
configureBindingin classBinder<BEAN>- Parameters:
binding- a binding to configuredefinition- a property definition information- Returns:
- the new configured binding
-
removeBindingInternal
Description copied from class:BinderRemoves (internally) theBindingfrom the bound properties map (if present) and from the list ofBindings. Note that this DOES NOT remove theValueChangeListenerthat theBindingmight have registered with anyHasValues or decouple theBinderfrom within theBinding. To do that, useBinder.Binding.unbind()This method should just be used for internal cleanup.- Overrides:
removeBindingInternalin classBinder<BEAN>- Parameters:
binding- TheBindingto remove from the binding map
-