com.vaadin.flow.data.binder.
Class Binder.BindingBuilderImpl<BEAN,FIELDVALUE,TARGET>
Type Parameters:
BEAN
- the bean type, must match the Binder bean type
FIELDVALUE
- the value type of the field
TARGET
- the target data type of the binding, matches the field type
until a converter has been set
All Implemented Interfaces:
Binder.BindingBuilder<BEAN,
, Serializable
Direct Known Subclasses:
An internal implementation of BindingBuilder
.
See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
BindingBuilderImpl
(Binder<BEAN> binder, HasValue<?, FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converterValidatorChain, BindingValidationStatusHandler statusHandler) Creates a new binding builder associated with the given field.
-
Method Summary
Modifier and TypeMethodDescriptionasRequired
(ErrorMessageProvider errorMessageProvider) Sets the field to be required.
asRequired
(Validator<TARGET> customRequiredValidator) Sets the field to be required and delegates the required check to a custom validator.
Completes this binding using the given getter and setter functions representing a backing bean property.
Completes this binding by connecting the field to the property with the given name.
bindReadOnly
(ValueProvider<BEAN, TARGET> getter) Completes this binding using the given getter function representing a backing bean property.
bindReadOnly
(String propertyName) Completes this binding by connecting the field to the property with the given name.
protected void
Throws if this binding is already completed and cannot be modified anymore.
Returns the
Binder
connected to thisBinding
instance.getField()
Gets the field the binding is being built for.
<NEWTARGET>
Binder.BindingBuilder<BEAN,NEWTARGET> withConverter
(Converter<TARGET, NEWTARGET> converter) Maps the binding to another data type using the given
Converter
.protected <NEWTARGET>
Binder.BindingBuilder<BEAN,NEWTARGET> withConverter
(Converter<TARGET, NEWTARGET> converter, boolean resetNullRepresentation) Implements
withConverter(Converter)
method with additional possibility to disable (reset) default null representation converter.withDefaultValidator
(boolean defaultValidatorEnabled) Sets up this binding to either enable or disable the default field validator (e.g.
withEqualityPredicate
(SerializableBiPredicate<TARGET, TARGET> equalityPredicate) Sets the
equalityPredicate
used to compare the current value of a field with its initial value.Sets a
BindingValidationStatusHandler
to track validation status changes.withValidator
(Validator<? super TARGET> validator) Adds a validator to this binding.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.data.binder.Binder.BindingBuilder
asRequired, asRequired, withConverter, withConverter, withNullRepresentation, withStatusLabel, withValidator, withValidator, withValidator, withValidator
-
Constructor Details
-
BindingBuilderImpl
protected BindingBuilderImpl(Binder<BEAN> binder, HasValue<?, FIELDVALUE> field, Converter<FIELDVALUE, TARGET> converterValidatorChain, BindingValidationStatusHandler statusHandler) Creates a new binding builder associated with the given field. Initializes the builder with the given converter chain and status change handler.
Parameters:
binder
- the binder this instance is connected to, not nullfield
- the field to bind, not nullconverterValidatorChain
- the converter/validator chain to use, not nullstatusHandler
- the handler to track validation status, not null
-
-
Method Details
-
bind
public Binder.Binding<BEAN,TARGET> bind(ValueProvider<BEAN, TARGET> getter, Setter<BEAN, TARGET> setter) Description copied from interface:
Binder.BindingBuilder
Completes this binding using the given getter and setter functions representing a backing bean property. The functions are used to update the field value from the property and to store the field value to the property, respectively.
When a bean is bound with
Binder.setBean(Object)
, the field value is set to the return value of the given getter. The property value is then updated via the given setter whenever the field value changes. The setter may be null; in that case the property value is never updated and the binding is said to be read-only.If the Binder is already bound to some bean, the newly bound field is associated with the corresponding bean property as described above.
If the bound field implements
HasValidator
, then the binding instance returned by this method will subscribe for field'sValidationStatusChangeEvent
s and willvalidate
itself upon receiving them.The getter and setter can be arbitrary functions, for instance implementing user-defined conversion or validation. However, in the most basic use case you can simply pass a pair of method references to this method as follows:
class Person { public String getName() { ... } public void setName(String name) { ... } } TextField nameField = new TextField(); binder.forField(nameField).bind(Person::getName, Person::setName);
Note: when a
null
setter is given the field will be marked as readonly by invokingHasValue.setReadOnly(boolean)
.Specified by:
bind
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
getter
- the function to get the value of the property to the field, not nullsetter
- the function to write the field value to the property or null if read-onlyReturns:
the newly created binding
-
bindReadOnly
Description copied from interface:
Binder.BindingBuilder
Completes this binding using the given getter function representing a backing bean property. The function is used to update the field value from the property. The field value is not written back to the bean so the binding is read-only.
When a bean is bound with
Binder.setBean(Object)
, the field value is set to the return value of the given getter.If the Binder is already bound to some bean, the newly bound field is associated with the corresponding bean property as described above.
The getter can be arbitrary functions, for instance implementing user-defined conversion or validation. However, in the most basic use case you can simply a method references to this method as follows:
Note: the field will be marked as readonly by invokingclass Person { public String getName() { ... } } TextField nameField = new TextField(); binder.forField(nameField).bindReadOnly(Person::getName);
HasValue.setReadOnly(boolean)
.This is a shorthand for
Binder.BindingBuilder.bind(ValueProvider, Setter)
method called withnull
setter.Specified by:
bindReadOnly
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
getter
- the function to get the value of the property to the field, not nullReturns:
the newly created binding
See Also:
-
bind
Description copied from interface:
Binder.BindingBuilder
Completes this binding by connecting the field to the property with the given name. The getter and setter of the property are looked up using a
PropertySet
.For a
Binder
created using theBinder(Class)
constructor, introspection will be used to find a Java Bean property. If a JSR-303 bean validation implementation is present on the classpath, aBeanValidator
is also added to the binding.The property must have an accessible getter method. It need not have an accessible setter; in that case the property value is never updated and the binding is said to be read-only. Nested property, when supported, can be referenced using the bean path, starting from the root class, for example 'address.streetName'. All intermediate getters must exist (e.g.
getAddress()
), and should never return null, otherwise binding will fail.Note: when the binding is read-only the field will be marked as readonly by invoking
HasValue.setReadOnly(boolean)
.Specified by:
bind
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
propertyName
- the name of the property to bind, not nullReturns:
the newly created binding
See Also:
-
bindReadOnly
Description copied from interface:
Binder.BindingBuilder
Completes this binding by connecting the field to the property with the given name. The getter of the property is looked up using a
PropertySet
. The field value is not written back to the bean so the binding is read-only.For a
Binder
created using theBinder(Class)
constructor, introspection will be used to find a Java Bean property. If a JSR-303 bean validation implementation is present on the classpath, aBeanValidator
is also added to the binding.The property must have an accessible getter method. Nested property, when supported, can be referenced using the bean path, starting from the root class, for example 'address.streetName'. All intermediate getters must exist (e.g.
getAddress()
), and should never return null, otherwise binding will fail.Note: the field will be marked as readonly by invoking
HasValue.setReadOnly(boolean)
.Specified by:
bindReadOnly
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
propertyName
- the name of the property to bind, not nullReturns:
the newly created binding
See Also:
-
withValidator
Description copied from interface:
Binder.BindingBuilder
Adds a validator to this binding. Validators are applied, in registration order, when the field value is written to the backing property. If any validator returns a failure, the property value is not updated.
Specified by:
withValidator
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
validator
- the validator to add, not nullReturns:
this binding, for chaining
See Also:
-
withConverter
public <NEWTARGET> Binder.BindingBuilder<BEAN,NEWTARGET> withConverter(Converter<TARGET, NEWTARGET> converter) Description copied from interface:
Binder.BindingBuilder
Maps the binding to another data type using the given
Converter
.A converter is capable of converting between a presentation type, which must match the current target data type of the binding, and a model type, which can be any data type and becomes the new target type of the binding. When invoking
Binder.BindingBuilder.bind(ValueProvider, Setter)
, the target type of the binding must match the getter/setter types.For instance, a
TextField
can be bound to an integer-typed property using an appropriate converter such as aStringToIntegerConverter
.The converted value is applied back to the field by default, this can be controlled with the method
Binder.Binding.setConvertBackToPresentation(boolean)
.Specified by:
withConverter
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Type Parameters:
NEWTARGET
- the type to convert toParameters:
converter
- the converter to use, not nullReturns:
this BindingBuilder configured with the specified converter
See Also:
-
withValidationStatusHandler
public Binder.BindingBuilder<BEAN,TARGET> withValidationStatusHandler(BindingValidationStatusHandler handler) Description copied from interface:
Binder.BindingBuilder
Sets a
BindingValidationStatusHandler
to track validation status changes.The validation state of each field is updated whenever the user modifies the value of that field.
This method allows to customize the way a binder displays error messages.
The method may be called only once. It means there is no chain unlike
Binder.BindingBuilder.withValidator(Validator)
orBinder.BindingBuilder.withConverter(Converter)
. Also it means that the shorthand methodBinder.BindingBuilder.withStatusLabel(HasText)
also may not be called after this method.Specified by:
withValidationStatusHandler
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
handler
- status change handlerReturns:
this binding, for chaining
See Also:
-
withDefaultValidator
Description copied from interface:
Binder.BindingBuilder
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. By default, all bindings will use the Binder-level setting if no value is set for them.
Specified by:
withDefaultValidator
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
defaultValidatorEnabled
- true to enable default validator for this binding, false to disable itReturns:
this binding, for chaining
See Also:
-
asRequired
Description copied from interface:
Binder.BindingBuilder
Sets the field to be required. This means two things:
- the required indicator is visible
- the field value is validated for not being empty*
HasValue.getEmptyValue()
returns.Specified by:
asRequired
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
errorMessageProvider
- the provider for localized validation error messageReturns:
this binding, for chaining
See Also:
-
asRequired
Description copied from interface:
Binder.BindingBuilder
Sets the field to be required and delegates the required check to a custom validator. This means two things:
- the required indicator will be displayed for this field
- the field value is validated by customRequiredValidator
Specified by:
asRequired
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
customRequiredValidator
- validator responsible for the required checkReturns:
this binding, for chaining
See Also:
-
withEqualityPredicate
public Binder.BindingBuilder<BEAN,TARGET> withEqualityPredicate(SerializableBiPredicate<TARGET, TARGET> equalityPredicate) Description copied from interface:
Binder.BindingBuilder
Sets the
equalityPredicate
used to compare the current value of a field with its initial value.By default it is null, meaning the initial value comparison is not active. Once it is set, the value of the field will be compared with its initial value. If the value of the field is set back to its initial value, it will not be considered as having uncommitted changes.
Specified by:
withEqualityPredicate
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Parameters:
equalityPredicate
- the predicate to use for equality comparisonReturns:
this
BindingBuilder
, for method chaining -
withConverter
protected <NEWTARGET> Binder.BindingBuilder<BEAN,NEWTARGET> withConverter(Converter<TARGET, NEWTARGET> converter, boolean resetNullRepresentation) Implements
withConverter(Converter)
method with additional possibility to disable (reset) default null representation converter.The method
withConverter(Converter)
calls this method withtrue
provided as the second argument value.Type Parameters:
NEWTARGET
- the type to convert toParameters:
converter
- the converter to use, not nullresetNullRepresentation
- iftrue
then default null representation will be deactivated (if not yet), otherwise it won't be removedReturns:
this BindingBuilder configured with the appropriate type
Throws:
IllegalStateException
- ifbind
has already been calledSee Also:
-
getBinder
Returns the
Binder
connected to thisBinding
instance.Returns:
the binder
-
checkUnbound
protected void checkUnbound()Throws if this binding is already completed and cannot be modified anymore.
Throws:
IllegalStateException
- if this binding is already bound -
getField
Description copied from interface:
Binder.BindingBuilder
Gets the field the binding is being built for.
Specified by:
getField
in interfaceBinder.BindingBuilder<BEAN,
FIELDVALUE> Returns:
the field this binding is being built for
-