easybinder
Easy bean binding for Vaadin 8 with JSR-303 support
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 (https://github.com/vaadin/framework/issues/9202).
- JSR 303 bean level validation support (https://github.com/vaadin/framework/issues/8498).
- JSR 303 validation groups support (https://github.com/vaadin/framework/issues/8385).
- JSR 303 @Valid support (https://github.com/vaadin/framework/issues/9520)
- unbind() method.
- Support for automatic binding of nested beans (https://github.com/vaadin/framework/issues/9210)
- Proper null-conversions (https://github.com/vaadin/framework/issues/8441, https://github.com/vaadin/framework/issues/9000 and https://github.com/vaadin/framework/issues/9453)
- Binding with value-provider supports JSR 303 validation (https://github.com/vaadin/framework/issues/8815)
- Properties are displayed in correct order in Grid's
- Grid property displays uses same converter sets as forms
- Automatic editor for Grid building
- "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);
Build a Grid
AutoBinder<MyEntity> binder = new AutoBinder<>(MyEntity.class);
binder.buildAndBind();
EGrid<Flight> grid = new EGrid<>(binder);
grid.setItems(..., ...);
grid.getEditor().setEnabled(true);
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.
Sample code
class MyEntity { @NotNull String name; int height; @Temporal(TemporalType.DATE) Date dateOfBirth; @Temporal(TemporalType.TIMESTAMP) Date timeAndDate; ... getters and setters... } 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);
class MyEntity { @NotNull String name; int height; @Temporal(TemporalType.DATE) Date dateOfBirth; @Temporal(TemporalType.TIMESTAMP) Date timeAndDate; ... getters and setters... } 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)
class MyEntity { @NotNull String name; int height; @Temporal(TemporalType.DATE) Date dateOfBirth; @Temporal(TemporalType.TIMESTAMP) Date timeAndDate; ... getters and setters... } AutoBinder<MyEntity> binder = new AutoBinder<>(MyEntity.class); binder.buildAndBind(); EGrid<Flight> grid = new EGrid<>(binder); grid.setItems(..., ...); grid.getEditor().setEnabled(true);
ConverterRegistry.getInstance().registerConverter(LocalDate.class, Date.class, new LocalDateToDateConverter());
ComponentFactoryRegistry.getInstance().addBuildPattern(boolean.class, e -> true, e -> new CheckBox(SharedUtil.camelCaseToHumanFriendly(e.getName())) );
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Highlights:
- Supports Java9+
- Supports recent versions of Vaadin 8
- Released
- 2021-09-17
- Maturity
- EXPERIMENTAL
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 8.0+
- Browser
- Browser Independent