Class BindingBuilder<BEAN,TARGET>
- Type Parameters:
BEAN- the bean typeTARGET- the current target type (changes when a converter is applied)
Binding between a form field and a bean property.
Obtain an instance from Binder.forField(InputElement) or
Binder.forCheckbox(com.oorian.html.elements.Checkbox), configure converters and
validators, then call bind(Function, BiConsumer) to complete the binding.
Example:
binder.forField(ageField)
.withConverter(new StringToIntegerConverter("Not a number"))
.withValidator(Validator.of(v -> v >= 0 && v <= 150, "Invalid age"))
.asRequired("Age is required")
.bind(Person::getAge, Person::setAge);
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionasRequired(String errorMessage) Marks this binding as required.Completes the binding with the given getter and setter.bindReadOnly(Function<BEAN, TARGET> getter) Completes the binding as read-only (no setter).<NEWTARGET>
BindingBuilder<BEAN, NEWTARGET> withConverter(Converter<TARGET, NEWTARGET> converter) Adds a type converter to this binding.withValidator(Validator<? super TARGET> validator) Adds a validator that will be run on the converted value during validation.
-
Method Details
-
withConverter
public <NEWTARGET> BindingBuilder<BEAN,NEWTARGET> withConverter(Converter<TARGET, NEWTARGET> converter) Adds a type converter to this binding.The converter transforms between the field's value type and the bean property type. Converters should be added before validators, since validators operate on the post-conversion type.
- Type Parameters:
NEWTARGET- the new target type after conversion- Parameters:
converter- the converter to apply- Returns:
- a new builder with the converted target type
-
withValidator
Adds a validator that will be run on the converted value during validation.Multiple validators can be added. They are evaluated in order and evaluation stops at the first failure.
- Parameters:
validator- the validator to add- Returns:
- this builder for chaining
-
asRequired
Marks this binding as required. A required field must have a non-empty value; for checkboxes, the checkbox must be checked.- Parameters:
errorMessage- the error message shown when the field is empty- Returns:
- this builder for chaining
-
bind
Completes the binding with the given getter and setter.The getter reads the property value from the bean (used by
Binder.readBean(Object)). The setter writes the converted field value back to the bean (used byBinder.writeBean(Object)).- Parameters:
getter- reads the property from the beansetter- writes the property to the bean- Returns:
- the created binding
-
bindReadOnly
Completes the binding as read-only (no setter).The field will be populated by
Binder.readBean(Object)but its value will not be written back to the bean byBinder.writeBean(Object).- Parameters:
getter- reads the property from the bean- Returns:
- the created binding
-