0

I have an enum:

public enum Roles {
   USER,ADMIN;
}

And a CheckBoxGroup on my form:

private CheckBoxGroup<String> rolesCheckbox = new CheckBoxGroup<>("User roles");

Instance of a User class binded to this form has a field:

private List<Roles> roles;

And what I want to do now is just check correct checkboxes when opening form for a specific User instance. I probably has to do something insinde binder.forField but I don't really know how:

binder.forField(rolesCheckbox).bind(...
Lokesh Kumar Gaurav
  • 726
  • 1
  • 8
  • 24
hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50

1 Answers1

8

The example from the Vaadin docs uses String for brevity, but you can easily use objects. Suppose you have a Role enum and a User like below:

// possible roles
public enum Role {
    ADMIN, USER, GUEST
}

// bean for binding
public class User {
    private Set<Role> roles;
    private String name;

    public User(String name, Set<Role> roles) {
        this.roles = roles;
        this.name = name;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "roles=" + roles +
                ", name='" + name + '\'' +
                '}';
    }
}

... you could create your form similar to:

public class CheckboxEnumBinding extends VerticalLayout {

    public CheckboxEnumBinding() {
        // form components
        TextField nameTextField = new TextField("Name");
        CheckBoxGroup<Role> roleCheckBoxGroup = new CheckBoxGroup<>("Roles", DataProvider.ofItems(Role.values()));

        // binder setup
        Binder<User> userBinder = new Binder<>();

        // name binding
        userBinder.forField(nameTextField)
                .asRequired("Please provide a user name")
                .bind(User::getName, User::setName);

        // role binding
        userBinder.forField(roleCheckBoxGroup)
                .asRequired("Please select at least one role")
                .bind(User::getRoles, User::setRoles);

        // bind to bean
        userBinder.setBean(new User("Morfic", EnumSet.of(Role.ADMIN, Role.GUEST)));

        // simulate a save action
        Button saveButton = new Button("Save", event -> Notification.show("Saved new user info: " + userBinder.getBean()));

        // add fields to the UI
        addComponents(nameTextField, roleCheckBoxGroup, saveButton);
    }
}

Result:

vaadin chekbox group binding


Bonus: By default Vaadin will use the enum name to display the values. If you want something different, like capitalizing only the first letter you can use an ItemCaptionGenerator:

// spring boot app, don't reinvent the wheel
import org.springframework.util.StringUtils;
...
roleCheckBoxGroup.setItemCaptionGenerator(role -> StringUtils.capitalize(role.name().toLowerCase()));

Vaadin item caption generator

Morfic
  • 15,178
  • 3
  • 51
  • 61