středa 17. června 2015

Vaadin 7 FieldGroup text box is read only

Today, I was experiencing 'strange' problem during implementation of form based on the BeanFieldGroup binder. It was quite stupid mistake at the end...

I have had following source code:
formLayout = new FormLayout();
binder = new BeanFieldGroup<>(Placement.class);
binder.setItemDataSource(this.placement);
Field<?> f = binder.buildAndBind("Placement name", "name");
formLayout.addComponent(f);
binder.setBuffered(true);

When I tried to execute project, I found out that 'Placement name' field in the form is not editable. So I debugged isEnabled() and isReadOnly() method of the 'f' field. Surprisingly 'isReadOnly()' method returned True  all the time.
The cause of the problem was quite simple. I've written following implementation of the 'Placement' entity:
public class Placement {
    private String name;
    public Placement() {
        this("");
    }
    public Placement(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}
If you're not as stupid as I was, you can see the problem already...yes, there is a setter method for 'name' property, so the framework automatically set the field as read only. Following implementation of Placement entity fixed the problem:

public class Placement {
    private String name;
    public Placement() {
        this("");
    }
    public Placement(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public String setName() {
        this.name=name;
    }
}

Žádné komentáře:

Okomentovat