Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Latest commit

 

History

History
54 lines (48 loc) · 2.06 KB

using-binder-in-review-editor-dialog.asciidoc

File metadata and controls

54 lines (48 loc) · 2.06 KB
title order layout
Using Binder in ReviewEditorDialog
2
page

Using Binder in ReviewEditorDialog

ReviewEditorDialog implements a form which lets the user edit the various properties of a Review object.

The logic related to properly displaying, converting and validating field values to and from the specific web components used to display may become quite complex and may also be repeated across different fields. The Binder can greatly simplify this.

Binder

Between the form and the backend data, Binder provides a bridge to connect the property names with the related values. Once the bindings are declared, you can use the readBean and writeBean methods to sync data.

//declare a Binder for review class
private Binder<Review> binder = new Binder<>();
...
// populate the dialog components with the current item's property values
binder.readBean(currentItem);

Furthermore, Binder offers an easy way to convert and validate the input data. Let’s take the property "Times Tasted" as an example:

private TextField timesTasted = new TextField();
...
getBinder().forField(timesTasted)
        .withConverter(
                new StringToIntegerConverter(0, "Must enter a number."))
        .withValidator(new IntegerRangeValidator(
                "The tasting count must be between 1 and 99.", 1, 99))
        .bind(Review::getCount, Review::setCount);

This code declares a converter to convert between the int property of the current item and the String text displayed in the text field. If the text can not be converted to an integer, the conversion fails and the given error message is displayed.

To further restrict the possible values for this field, a validator is also declared to ensure that the values is between 1 and 99.

For more information related to Binder, you may check the Tutorial for Binder.