Reygok3
Reygok3

Reputation: 79

Vaadin BeanFieldGroup - "Property is not cascaded" when binding nested property

I've been searching for hours on here and on the Vaadin forums, but I seem to have a unique problem here.

I simplified my problem a lot to be able to illustrate it easily. So I have a Bean class:

public class Bean {
    private String name;
    private NestedBean nestedBean;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public NestedBean getNestedBean() {
        return nestedBean;
    }

    public void setNestedBean(NestedBean nestedBean) {
        this.nestedBean = nestedBean;
    }

    Bean() {
        this.name = "Bean";
        this.nestedBean = new NestedBean();
    }
}

And its nested field, class NestedBean:

public class NestedBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    NestedBean() {
        this.name = "NestedBean";
    }
}

So now I want to bind an instance of Bean to two TextFields, with the help of a BeanFieldGroup:

Bean bean = new Bean();

BeanFieldGroup<Bean> binder = new BeanFieldGroup<>(Bean.class);
binder.setItemDataSource(bean);

addComponent(binder.buildAndBind("Name", "name"));
addComponent(binder.buildAndBind("Nested name", "nestedBean.name"));

This, however, throws this exception:

java.lang.IllegalArgumentException: Property com.reygok.vaadin.main.Bean.nestedBean is not cascaded

Caused by: org.apache.bval.jsr.UnknownPropertyException: Property com.reygok.vaadin.main.Bean.nestedBean is not cascaded

I tried different solutions, like:

Creating the TextFields first and then using

binder.bind(textField, "nestedBean.name");

Doing this first:

binder.getItemDataSource().addNestedProperty("nestedBean.name");

But nothing changed the Exception. So does someone know what causes this?

Thanks a lot in advance!

Upvotes: 0

Views: 319

Answers (2)

Reygok3
Reygok3

Reputation: 79

I found it, so if others have the same problem:

The solution is to add the @Valid annotation to the fields that have nested fields inside of them.

So in my example:

public class Bean {
   private String name;

   @Valid
   private NestedBean nestedBean;
...

Upvotes: 2

fla
fla

Reputation: 143

I recommand you to bind member before setting bean data source

BeanFieldGroup<Bean> binder = new BeanFieldGroup<>(Bean.class);

// first
addComponent(binder.buildAndBind("Name", "name"));
addComponent(binder.buildAndBind("Nested name", "nestedBean.name"));
// then
binder.setItemDataSource(bean);

Upvotes: 0

Related Questions