Loading From and Saving To Business Objects

Once your bindings are set up, you are ready to fill the bound UI components with data from your business objects.

Changes can be written to business objects automatically or manually.

Reading and Writing Automatically

Writing to business objects automatically when the user makes changes in the UI is ususally the most convenient option.

You can bind the values directly to an instance, by allowing Binder to automatically save values from the fields.

Example: Automatically saving field values.

Java

  1. Binder<Person> binder = new Binder<>();
  2. // Field binding configuration omitted,
  3. // it should be done here
  4. Person person = new Person("John Doe", 1957);
  5. // Loads the values from the person instance
  6. // Sets person to be updated when any bound field
  7. // is updated
  8. binder.setBean(person);
  9. Button saveButton = new Button("Save", event -> {
  10. if (binder.validate().isOk()) {
  11. // person is always up-to-date as long as
  12. // there are no validation errors
  13. MyBackend.updatePersonInDatabase(person);
  14. }
  15. });
  • The validate() call ensures that bean-level validators are checked when saving automatically.
Warning
When you use the setBean method, the business object instance updates whenever the user changes the value of a bound field. If another part of the application simultaneously uses the same instance, that part could display changes before the user saves. You can prevent this by using a copy of the edited object, or by manually writing to only update the object when the user saves.

Reading Manually

You can use the readBean method to manually read values from a business object instance into the UI components.

Example: Using the readBean method.

Java

  1. Person person = new Person("John Doe", 1957);
  2. binder.readBean(person);
  • This example assumes that binder has been configured with a TextField bound to the name property.

  • The value “John Doe” displays in the field.

Validating and Writing Manually

To prevent displaying multiple errors to the user, validation errors only display after the user has edited each field and submitted (loaded) the form.

You can explicitly validate the form or attempt to save the values to a business object, even if the user has not edited a field.

Example: Explicitly validating a form.

Java

  1. // This will make all current validation errors visible
  2. BinderValidationStatus<Person> status =
  3. binder.validate();
  4. if (status.hasErrors()) {
  5. notifyValidationErrors(status.getValidationErrors());
  6. }

Writing the field values to a business object fails if any of the bound fields contain an invalid value. You can deal with invalid values in a number of different ways:

Example: Handling a checked exception.

Java

  1. try {
  2. binder.writeBean(person);
  3. MyBackend.updatePersonInDatabase(person);
  4. } catch (ValidationException e) {
  5. notifyValidationErrors(e.getValidationErrors());
  6. }

Example: Checking a return value.

Java

  1. boolean saved = binder.writeBeanIfValid(person);
  2. if (saved) {
  3. MyBackend.updatePersonInDatabase(person);
  4. } else {
  5. notifyValidationErrors(binder.validate()
  6. .getValidationErrors());
  7. }

Example: Adding bean-level validators.

Java

  1. binder.withValidator(
  2. p -> p.getYearOfMarriage() > p.getYearOfBirth(),
  3. "Marriage year must be bigger than birth year.");
  • The withValidator(Validator) method runs on the bound bean after update of the values of the bound fields.

  • Bean-level validators also run as part of writeBean(Object), writeBeanIfValid(Object) and validate(Object), if the content passes all field-level validators.

Note
For bean-level validators, the bean must be updated before the validator runs. If a bean-level validator fails in writeBean(Object) or writeBeanIfValid(Object), the bean reverts to the state it was in before returning from the method. Remember to check your getters/setters to ensure there are no unwanted side effects.

Tracking Binding Status

Binder keeps track of which bindings have been updated by the user and which bindings are in an invalid state. It fires an event when there are status changes. You can use this event to appropriately enable and disable the form buttons, depending on the current status of the form.

Example: Enabling the save and reset buttons when changes are detected.

Java

  1. binder.addStatusChangeListener(event -> {
  2. boolean isValid = event.getBinder().isValid();
  3. boolean hasChanges = event.getBinder().hasChanges();
  4. saveButton.setEnabled(hasChanges && isValid);
  5. resetButton.setEnabled(hasChanges);
  6. });