FormBinder nested property

I’m not able to bind nested attributes like

POJO Vendor


class Vendor{
  private String name;
  private Address address

  ...
  getters/setters
  ...
}

POJO Address


class Address{
  private String street;
  ...
  getters/setters
  ...
}

bind the form to the datasource


vendor = new Vendor();
vendor.setAddress(new Address());

form = new ViewBoundForm(new VendorFormView());
form.setImmediate(true);
form.setItemDataSource(new BeanItem<Vendor>(vendor));
formLayout.addComponent(form);

Form 1

	
@AutoGenerated
@PropertyId("address.street")
private TextField streetField;

Form 2


@AutoGenerated
private TextField streetField;

Form 1 and 2 are working for all not nested attributes like the vendors name.

Is there a way to bind nested attributes like Vendor.address.street?

Hi,

I have never tried anything like this, but I think it should work if you first introduce the nested property in the BeanItem that you are assigning in to the form. Support for “nested properties” in BeanItems was introduced in Vaadin 6.6, but as far as I know it doesn’t work automatically.

Something like this might do the trick:

vendorItem = new BeanItem(vendor);
vendorItem.addItemProperty(“address.street”, new NestedMethodProperty(vendor, “address.street”));

cheers,
matti

Quick comment on what Matti has suggested.

We have just put in place in one of our projects the NestedProperties in combination with FormBinder and it works perfectly.
One minor detail is eventually if you want to “automate” the binding and addition of nested properties. In such as case, here is a hint on how
to proceded. Here is an automatic way to add nested properties….

    BeanItem<Person> personItem = new BeanItem<Person>(this.person);
    try {
      Map<String,String> beanMap = MyBeanUtils.recursiveDescribe(person);
      for (Entry<String, String> entry : beanMap.entrySet()) {
        personItem.addItemProperty(entry.getKey(), new NestedMethodProperty(this.person, entry.getKey()));
      }
    } catch (Exception e1) {
      throw new RuntimeException("Exception not handled: " + e1, e1);
    }

where MyBeanUtils is an adapatation of the following thread http://stackoverflow.com/questions/6133660/recursive-beanutils-describe

hope this may help.

E.