BeanFieldGroups, Validation and initial null values

Hi,

I have a class Person an a class Group:

[code]
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 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;
}

}
[/code]And I have on the GUI side a combobox for selecting the boss:

[code]
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 beanBinder = new BeanFieldGroup<>(Person.class);
beanBinder.setItemDataSource(group);
beanBinder.setBuffered(false);
beanBinder.bind(cbbTest, “boss”);

// add validation
cbbTest.addValidator(new MyValidator());
[/code]The validator method looks like:

[code]
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());
}
[/code]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());
}