EasyBinder
EasyBinder is an alternative field binder for Vaadin 8 that tries to reduce boiler plate code by automating the binding process and relying on javax.validation (JSR 303) for validation.
New features compared with Vaadin 8 standard binder:
- Converter registration support (Binder should automatically configure built in converters · Issue #9202 · vaadin/framework · GitHub).
- JSR 303 bean level validation support (BeanValidationBinder should automatically add a JSR-303 bean-level validator · Issue #8498 · vaadin/framework · GitHub).
- JSR 303 validation groups support (BeanBinder bean validation groups support · Issue #8385 · vaadin/framework · GitHub).
- JSR 303 @Valid support (BeanValidationBinder / BeanValidator / getJavaxBeanValidator().validateValue() does not honor @Valid · Issue #9520 · vaadin/framework · GitHub)
- unbind() method.
- Support for automatic binding of nested beans (Binder.bindInstanceFields should bind sub properties · Issue #9210 · vaadin/framework · GitHub)
- Proper null-conversions (Add generic support for "reverse" null representations · Issue #8441 · vaadin/framework · GitHub, BeanValidationBinder says the form is valid even though required (@NotNull) field is not filled · Issue #9000 · vaadin/framework · GitHub and BeanValidationBinder reports valid even though using validation API directly reports invalid · Issue #9453 · vaadin/framework · GitHub)
- Binding with value-provider supports JSR 303 validation (Update documentation to explicitly mention that JSR-303 annotations won't be used when binding with ValueProvider. · Issue #8815 · vaadin/framework · GitHub)
- “Naked objects” inspired automatic form-builder.
- Easily extendable, most internal classes, methods and fields are declared protected.
Limitations compared with Vaadin 8 standard binder:
- No readBean()/writeBean() support, only setBean() is supported.
- No fluent-api binder builder (but can be easily added).
Usage
Given the following Entity:
class MyEntity {
@NotNull
String name;
int height;
@Temporal(TemporalType.DATE)
Date dateOfBirth;
@Temporal(TemporalType.TIMESTAMP)
Date timeAndDate;
... getters and setters...
}
It can be bound using one of the following approaches:
Method 1: Auto binding to existing Form fields
class MyForm {
TextField name = new TextField("Name");
TextField height = new TextField("Height");
DateField dateOfBirth = new DateField("Date of birth");
DateTimeField timeAndDate = new DateTimeField("Time and date");
}
MyForm form = new MyForm();
AutoBinder<MyEntity> binder = new AutoBinder<>(MyEntity.class);
// Perform binding
binder.bindInstanceFields(form);
// Add components to form
addComponents(binder.getBoundFields());
// Set entity
MyEntity entity = new MyEntity();
binder.setBean(entity);
Method 2: Auto creation of Form fields
AutoBinder<MyEntity> binder = new AutoBinder<>(MyEntity.class);
// Perform field creation, binding and add components to form
addComponents(
binder.buildAndBind()
);
// Set entity
MyEntity entity = new MyEntity();
binder.setBean(entity);
Register custom converters and builders
ConverterRegistry.registerConverter() can be used to register custom converters.
ComponentFactoryRegistry().addBuildPattern() can be used to register custom UI component factories.