> Markdown version of [Data Binding](https://vaadin.com/docs/v23/binding-data). Section index: [llms.txt](https://vaadin.com/docs/v23/llms.txt)

# Data Binding

Vaadin Flow includes mechanisms to bind field components as well as forms consisting of fields directly to business objects, such as Java beans. Binding is buffered, so you can load and reload data from the source to fields, and then save the edited data back.

This example has a view with two editors, one writable, one read-only. When the user clicks **Save**, the form data is written to the `BusinessPerson` object, which is reflected in the read-only form.

`DataBinding.java`

```java
// Data model
BusinessPerson person = new BusinessPerson();

// Two editors: the actual editor and read-only display
PersonEditor editor1 = new PersonEditor(false);
PersonEditor editor2 = new PersonEditor(true);

// The editor and buttons
VerticalLayout layout1 = new VerticalLayout();
HorizontalLayout buttons = new HorizontalLayout();
Button saveButton = new Button("Save");
saveButton.addClickListener(click -> {
    try {
        editor1.getBinder().writeBean(person);
        editor2.getBinder().readBean(person);
    } catch (ValidationException e) {
    }
});
Button resetButton = new Button("Reset");
resetButton.addClickListener(click -> {
    editor1.getBinder().readBean(person);
});
buttons.add(saveButton, resetButton);
layout1.add(editor1, buttons);

VerticalLayout layout2 = new VerticalLayout();
layout2.add(editor2);

// Show the two editors side-by-side
HorizontalLayout hlayout = new HorizontalLayout();
hlayout.setDefaultVerticalComponentAlignment(
        FlexComponent.Alignment.START);
hlayout.add(layout1, layout2);
add(hlayout);
```

`PersonEditor.java`

```java
public class PersonEditor extends FormLayout {
    private Binder<BusinessPerson> binder;

    public PersonEditor(boolean readOnly) {
        // For binding the form to the data model
        binder = new Binder<>(BusinessPerson.class);

        TextField nameField = new TextField();
        addFormItem(nameField, "Name");
        binder.bind(nameField, BusinessPerson::getName,
                BusinessPerson::setName);
        nameField.setReadOnly(readOnly);

        TextField titleField = new TextField();
        addFormItem(titleField, "Title");
        binder.forField(titleField).bind(BusinessPerson::getTitle,
                BusinessPerson::setTitle);
        titleField.setReadOnly(readOnly);
    }

    public Binder<BusinessPerson> getBinder() {
        return binder;
    }
}
```

`BusinessPerson.java`

```java
public class BusinessPerson {
    String name;
    String title;
    int age;

    public BusinessPerson() {
    }

    void setName(String name) {
        this.name = name;
    }

    String getName() {
        return name;
    }

    void setTitle(String title) {
        this.title = title;
    }

    String getTitle() {
        return title;
    }

    void setAge(int age) {
        this.age = age;
    }

    int getAge() {
        return age;
    }
}
```

Field and form input can be validated before saving. Additionally, when loading and saving, the data representation may need to be converted between the field type and the underlying data types in the business objects.

## <a id="topics"></a>Topics

- [Binding Data to Forms](https://vaadin.com/docs/v23/binding-data/components-binder.md): How to bind data to forms, which are composites with multiple fields that each bind to sub-structures in data.
- [Validating and Converting User Input](https://vaadin.com/docs/v23/binding-data/components-binder-validation.md): Like individual fields, forms with multiple fields can be validated before saving the input to the bound business objects.
- [Loading from and Saving to Business Objects](https://vaadin.com/docs/v23/binding-data/components-binder-load.md): Form state is loaded from business objects, and can be saved, normally after validation.
- [Binding Beans to Forms](https://vaadin.com/docs/v23/binding-data/components-binder-beans.md): Beans are the standard Java model for business objects. This article describes how they are bound to forms.
- [Binding Items to Components](https://vaadin.com/docs/v23/binding-data/data-provider.md): Selection components allow selecting a field value from a list of options. This article describes how they are bound and displayed in such components.
- [Creating a Component that Has a Value](https://vaadin.com/docs/v23/binding-data/field.md): How to create a field component.
