Binding Beans to Forms

Business objects are typically implemented as JavaBeans in an application. Binder supports binding the properties of a business object to UI components in your forms.

Manual Data Binding

You can use reflection based on bean property names to bind values. This reduces the amount of code needed when binding to fields in the bean.

Examples: Binding using reflection based on bean property names.

Java

  1. Binder<Person> binder = new Binder<>(Person.class);
  2. // Bind based on property name
  3. binder.bind(nameField, "name");
  4. // Bind based on sub property path
  5. binder.bind(streetAddressField, "address.street");
  6. // Bind using forField for additional configuration
  7. binder.forField(yearOfBirthField)
  8. .withConverter(
  9. new StringToIntegerConverter(
  10. "Please enter a number"))
  11. .bind("yearOfBirth");
Note
Be cautious when using strings to identify properties. A typo in the string, or a subsequent changes to the setter and getter method names, will result in a runtime exception.

Automatic Data Binding

The bindInstanceFields method facilitates automatic data binding.

UI fields are typically defined as members of a UI Java class. This allows you to access the fields easily using the different methods made available by the class. In this scenario, binding the fields is also simple, because when you pass the object to the UI class, the bindInstanceFields method matches the fields of the object to the properties of the related business object, based on their names.

Example: Using the bindInstanceFields method to bind all fields in a UI class.

Java

  1. public class MyForm extends VerticalLayout {
  2. private TextField firstName =
  3. new TextField("First name");
  4. private TextField lastName =
  5. new TextField("Last name");
  6. private ComboBox<Gender> gender =
  7. new ComboBox<>("Gender");
  8. public MyForm() {
  9. Binder<Person> binder =
  10. new Binder<>(Person.class);
  11. binder.bindInstanceFields(this);
  12. }
  13. }
  • This binds the firstName text field to the “firstName” property in the item, lastName text field to the “lastName” property, and the gender combo box to the “gender” property.

Without this method, it would be necessary to bind each field separately.

Example: Binding each field separately.

Java

  1. binder.forField(firstName)
  2. .bind(Person::getFirstName, Person::setFirstName);
  3. binder.forField(lastName)
  4. .bind(Person::getLastName, Person::setLastName);
  5. binder.forField(gender)
  6. .bind(Person::getGender, Person::setGender);

Specifying Property Names

The bindInstanceFields method processes all Java member fields with a type that extends HasValue (such as TextField) that can be mapped to a property name.

If the field name does not match the corresponding property name in the business object, you can use the @PropertyId annotation to specify the property name.

Example: Using the @PropertyId annotation to specify the “sex” property for the gender field.

Java

  1. @PropertyId("sex")
  2. private ComboBox<Gender> gender =
  3. new ComboBox<>("Gender");

Configuring Converters and Validators

When using the automatic bindInstanceFields method to bind fields, all converters and validators must be configured beforehand using a special forMemberField configurator. It works similar to the forField method, but it requires no explicit call to a bind method. If the bindInstanceFields method finds incompatible property-field pairs, it throws an IllegalStateException.

Alternatively, you can bind properties that need validators manually and then bind all remaining fields using the bindInstanceFields method. This method skips the properties that have already been bound manually.

Example: Manually specifying StringToIntegerConverter before calling the bindInstanceFields method.

Java

  1. TextField yearOfBirthField =
  2. new TextField("Year of birth");
  3. binder.forField(yearOfBirthField)
  4. .withConverter(
  5. new StringToIntegerConverter("Must enter a number"))
  6. .bind(Person::getYearOfBirth, Person::setYearOfBirth);
  7. binder.bindInstanceFields(this);

If you use JSR-303 validators, you should use BeanValidationBinder that picks validators automatically when using bindInstanceFields.

Using JSR 303 Bean Validation

You can use BeanValidationBinder if you prefer to use JSR 303 Bean Validation annotations such as Max, Min, Size, etc.

BeanValidationBinder extends Binder (and therefore has the same API), but its implementation automatically adds validators based on JSR 303 constraints.

To use Bean Validation annotations, you need a JSR 303 implementation like Hibernate Validator available in your classpath. If your environment, such as Java EE container, does not provide the implementation, you can use the following dependency in Maven:

XML

  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate-validator</artifactId>
  4. <version>5.4.1.Final</version>
  5. </dependency>

Defining Constraints for Properties

Example: Using JSR 303 Bean Validation annotations with BeanValidationBinder

Java

  1. public class Person {
  2. @Max(2000)
  3. private int yearOfBirth;
  4. // Non-standard constraint provided by
  5. // Hibernate Validator
  6. @NotEmpty
  7. private String name;
  8. // + other fields, constructors, setters and getters
  9. }
  10. BeanValidationBinder<Person> binder =
  11. new BeanValidationBinder<>(Person.class);
  12. binder.bind(nameField, "name");
  13. binder.forField(yearOfBirthField)
  14. .withConverter(
  15. new StringToIntegerConverter("Enter a number"))
  16. .bind("yearOfBirth");

Constraints defined for properties in the bean, work in the same way as if configured programmatically when the binding is created. For example, the following code snippets have the same result:

Example: Declarative Bean Validation annotation.

Java

  1. public class Person {
  2. @Max(value = 2000, message =
  3. "Year of Birth must be less than or equal to 2000")
  4. private int yearOfBirth;

Example: Programmatic validation using Binder specific API.

Java

  1. binder.forField(yearOfBirthField)
  2. .withValidator(
  3. yearOfBirth -> yearOfBirth <= 2000,
  4. "Year of Birth must be less than or equal to 2000")
  5. .bind(Person::getYearOfBirth, Person::setYearOfBirth);
Note
As an alternative to defining constraint annotations for specific properties, you can define constraints on the bean level, but Vaadin’s BeanValidationBinder does not currently support them. It simply ignores all JSR 303 validations that are not assigned directly to properties.

Automatically Marking Form Fields as Required

Some built-in validators in the bean validation API suggest that a value is required in input field. BeanValidationBinder automatically enables the visual “required” indicator using the HasValue.setRequiredIndicatorVisible(true) method for properties annotated with such validators. By default, @NotNull, @NotEmpty and @Size (if min() value is greater than 0) configures the field as required. You can change this behavior using the BeanValidationBinder.setRequiredConfigurator method.

Example: Overriding the default @Size behavior.

Java

  1. binder.setRequiredConfigurator(
  2. RequiredFieldConfigurator.NOT_EMPTY
  3. .chain(RequiredFieldConfigurator.NOT_NULL));