Class BindingBuilder<BEAN,TARGET>

java.lang.Object
com.oorian.data.binding.BindingBuilder<BEAN,TARGET>
Type Parameters:
BEAN - the bean type
TARGET - the current target type (changes when a converter is applied)

public class BindingBuilder<BEAN,TARGET> extends Object
Fluent builder for creating a 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 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

      public BindingBuilder<BEAN,TARGET> withValidator(Validator<? super TARGET> validator)
      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

      public BindingBuilder<BEAN,TARGET> asRequired(String errorMessage)
      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

      public Binding<BEAN,TARGET> bind(Function<BEAN,TARGET> getter, BiConsumer<BEAN,TARGET> setter)
      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 by Binder.writeBean(Object)).

      Parameters:
      getter - reads the property from the bean
      setter - writes the property to the bean
      Returns:
      the created binding
    • bindReadOnly

      public Binding<BEAN,TARGET> bindReadOnly(Function<BEAN,TARGET> getter)
      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 by Binder.writeBean(Object).

      Parameters:
      getter - reads the property from the bean
      Returns:
      the created binding