BeanValidationBinder - how to bind fields from object

Hi, without any problem I can bind object fields which are directly available (as field) in the object I am referring to. Well at least this is done
autmatically.

class Person {
String name;
String surname;
}

the binding via new BeanValidationBinder<>(Person.class) works great.

What if:
class Person {
String name;
String surname;
Address address;
}

Now I what the binding to the text field on an attribute from Address. Text field id is street, so I tried (Address:getStreet, Address:setStreet) but this results in an error: java.lang.IllegalStateException: Property street ‘com.example.myapplication.samples.backend.data.Address’ doesn’t match the field type ‘java.lang.String’. Binding should be configured manually using converter

So my question is: how to I bind attributes from Person to my text field?

Try binding to “address.street”.

Hi,

One way to do it so use labmda expressions, as it is explained in the
vaadin 8 data binding docs
.

For example, if you have the following classes.

[code]
public class Person {

String name;
String surname;
Address address;

// assume getters and setters
}
[/code][code]
public class Address {

String street;

// assume getter and setters
}
[/code]Then, you could bind the street address like this:

Binder<Person> binder = new Binder<>();

TextField streetAddressField = new TextField();

// bind using lambda expressions

binder.bind(streetAddressField,
 person -> person.getAddress().getStreet(),
 (person, street) -> person.getAddress().setStreet(street));

Hope this helps,
Goran

wonderful, didnt find that in the docs… works! Thank you :slight_smile:

that didnt work for me, complain is that the corresponding type address.street could not be solved. thx anyway :slight_smile: