How to data bind with a MultiSelectComboBox

After many hours, I was finally able to bind with a Select<…> using bind.withConverter. But I am stumped on how to bind with a MultiSelectComboBox.

I have a person document(MongoDB).

class Person {
  String id;
   :
}

and a Practice document.

class Practice {
  private List<Person> attendees;
  :
}

My edit form has the following defined:

Binder<Practice> binder = new BeanValidationBinder<>(Practice.class);

MultiSelectComboBox<Person> attendees = new MultiSelectComboBox<>();

binder.forField(attendees)
    .withConverter(new StringToPersonConverter(personService))
    .bind(Practice::getAttendees, Practice::setAttendees);

Finally, my StringToPersonConverter

public class StringToPersonConverter implements Converter<Set<Person>, String> {
    PersonService personService;

    public StringToPersonConverter(PersonService personService) {
        this.personService = personService;
    }

    @Override
    public Result<String> convertToModel(Set<Person> people, ValueContext valueContext) {
        return Result.ok(String.join(",", people.stream()
            .map(Person::getId)
            .collect(Collectors.toSet())));
    }

    @Override
    public Set<Person> convertToPresentation(String personId, ValueContext valueContext) {
        if (StringUtils.isNotBlank(personId)) {
            // what now?
            // return personService.getAll(personId);
        }
        return null;
    }
}

I’m probably way off with my approach. I sure could use some help. The tutorial and javadoc were not much help. I even looked at StringToUuidConverter just to get some hints. My converter is way off, I’m sure.

Any help would greatly appreciated. Thanks!

I’ve got the feeling you are doing something weirdly :sweat_smile: you don’t need the converter at all.

Normally it’s done like this:

Bean with Set :white_check_mark:
MultiSelect… :white_check_mark:
select.setItems(service.loadData()) :exclamation:
select.setItemLabel… :exclamation:
binder.forField(select) get/setAttendees :white_check_mark:

My first mistake was in my Practice class. Should have been:

class Practice {
  private Set<String> attendees;
  :
}

I want to store a set of Person IDs, which are Strings in MongoDB. From there, I just need custom renders and such. And I think you’re right, don’t need a converter, from what I can see now that I’ve changed from Person to String.

After a super late night and early morning coding session, I got it working.
image.png