Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
BeanFieldGroups, Validation and initial null values
Hi,
I have a class Person an a class Group:
public class Person {
private String name;
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Group {
private List<Person> members = new ArrayList<>();
private Person boss = null;
public List<Person> getMembers() {
return members;
}
public void setMembers(List<Person> members) {
this.members = members;
}
public Person getBoss() {
return boss;
}
public void setBoss(Person boss) {
this.boss = boss;
}
}
And I have on the GUI side a combobox for selecting the boss:
private ComboBox cbbTest = new ComboBox();
cbbTest.setImmediate(true);
...
viewLayout.addComponent(cbbTest);
cbbTest.setContainerDataSource(new BeanItemContainer<>(UploadFilesValidatorBean.Person.class, group.getMembers()));
// binding component to Group.boss
BeanFieldGroup<Person> beanBinder = new BeanFieldGroup<>(Person.class);
beanBinder.setItemDataSource(group);
beanBinder.setBuffered(false);
beanBinder.bind(cbbTest, "boss");
// add validation
cbbTest.addValidator(new MyValidator());
The validator method looks like:
public void validate(Object value) throws InvalidValueException {
if (group.getBoss() == null) {
System.out.println("Boss not set!");
throw new InvalidValueException("Boss not set!");
}
System.out.println("Boss is " + group.getBoss().getName());
}
This works great if the group.boss attribute is initial set to a valid value:
Group group = new Group();
group.getMembers().add(new Person("aa"));
group.getMembers().add(new Person("bb"));
group.getMembers().add(new Person("cc"));
group.setBoss(group.getMembers().get(0));
Every time if the user changes the selection of the combobox the validator method was called and group.boss is set to the currently selected value.
But if the group.boss value is initial set to null (group.setBoss(null)) the data binding don't work's correctly. Every time if the user change the combobox selection the validator method was called, but the group.boss attribute is still set to null.
Is there anything wrong or missing in my code? Whats the correct way to work with initial null values and data binding?
My current Vaadin version is 7.7.5
Many thanks,
Steffen
fdgd
Ok. Few minutes after posting found the error ....
For validating the value I must use the "value" parameter of the validate method and not (like in my case) the attribute of the group object. So the corrected validate method should be like:
public void validate(Object value) throws InvalidValueException {
// WRONG: if (group.getBoss() == null) {
if (value == null) {
System.out.println("Boss not set!");
throw new InvalidValueException("Boss not set!");
}
System.out.println("Boss is " + ((Person) value).getBoss().getName());
}