Class Binder<BEAN>
- Type Parameters:
BEAN- the bean type
A Binder manages a set of Binding objects, each linking one
InputElement to one bean property. Use readBean(Object) to populate
form fields from a bean and writeBean(Object) to write validated field values
back.
Basic usage:
Binder<Person> binder = new Binder<>(Person.class);
binder.forField(nameField)
.asRequired("Name is required")
.bind(Person::getName, Person::setName);
binder.forField(ageField)
.withConverter(new StringToIntegerConverter("Not a number"))
.bind(Person::getAge, Person::setAge);
binder.forCheckbox(activeCheckbox)
.bind(Person::isActive, Person::setActive);
// Populate form
binder.readBean(person);
// Write back (validates first)
binder.writeBean(person);
Auto-binding by field name:
binder.bindInstanceFields(myFormView);
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbindInstanceFields(Object view) Automatically bindsInputElementfields in the view object to bean properties by matching field names to getter/setter methods on the bean class.forCheckbox(Checkbox<?> field) Starts building a binding for a checkbox field.forField(InputElement<?> field) Starts building a binding for a text-based input field or select.Returns an unmodifiable list of all bindings.booleanReturns whether any bound field value has changed since the lastreadBean(Object).booleanisValid()Validates all bindings and returns whether all pass.voidPopulates all bound fields from the given bean.voidremoveBinding(InputElement<?> field) Removes the binding for the given field.validate()Validates all bindings and returns the results keyed by field.voidValidates all bindings and writes field values to the given bean.booleanwriteBeanIfValid(BEAN bean) Validates all bindings and writes field values to the bean only if all are valid.
-
Constructor Details
-
Binder
Creates a new Binder for the specified bean type.- Parameters:
beanType- the bean class (used for reflection-based auto-binding)
-
-
Method Details
-
forField
Starts building a binding for a text-based input field or select.If the given field is a
Select, the binding will automatically useSelect.getSelectedValue()andSelect.setSelectedValue(Object)for value access.- Parameters:
field- the input element to bind- Returns:
- a binding builder with
Stringas the initial target type
-
forCheckbox
Starts building a binding for a checkbox field.The binding reads and writes
Booleanvalues usingCheckbox.isChecked()andCheckbox.setChecked(boolean).- Parameters:
field- the checkbox to bind- Returns:
- a binding builder with
Booleanas the initial target type
-
readBean
Populates all bound fields from the given bean.Reads each bound property from the bean, applies the converter's
convertToPresentationmethod (if any), and sets the field's value.- Parameters:
bean- the bean to read property values from
-
writeBean
Validates all bindings and writes field values to the given bean.If any binding fails validation, a
BindingValidationExceptionis thrown containing all field errors. No values are written to the bean on failure.- Parameters:
bean- the bean to write property values to- Throws:
BindingValidationException- if one or more bindings fail validation
-
writeBeanIfValid
Validates all bindings and writes field values to the bean only if all are valid.- Parameters:
bean- the bean to write property values to- Returns:
trueif all bindings passed validation and values were written
-
isModified
public boolean isModified()Returns whether any bound field value has changed since the lastreadBean(Object).- Returns:
trueif at least one field has been modified
-
isValid
public boolean isValid()Validates all bindings and returns whether all pass.- Returns:
trueif all bindings are valid
-
validate
Validates all bindings and returns the results keyed by field.- Returns:
- a map of field-to-validation-result entries
-
removeBinding
Removes the binding for the given field.- Parameters:
field- the field whose binding should be removed
-
getBindings
Returns an unmodifiable list of all bindings.- Returns:
- the list of bindings
-
bindInstanceFields
Automatically bindsInputElementfields in the view object to bean properties by matching field names to getter/setter methods on the bean class.For each declared field in the view class that is an
InputElement, this method looks for a matching getter (getXxx()orisXxx()) and setter (setXxx()) on the bean type. If a type converter is needed (e.g., the field holds aStringbut the bean property is anInteger), one is looked up automatically from theConverterRegistry.Fields that have no matching bean property or no available converter are silently skipped.
- Parameters:
view- the object containingInputElementfields to bind
-