Is there any way to properly bind to a Set on a form?
I’m doing POJO binding and my controller takes in a User object
public static void create(User user)
user.java
public class User implements Serializable {
public Long id;
@Required
@Email
public String email;
public Set<Group> groups;
}
Group.java
public class Group implements Serializable {
public Long id;
public String name;
}
I can’t seem to get my field to bind to groups
i’ve tried user.groups[].id, user.groups[0].id, user.groups.id. I can get it to work with a list just fine but when it posts I get a list of N elements with a bunch of null items (one null for each checkbox that was not checked) and I could just create a new list without the nulls but that seems wrong.
Edit: User and Group are not meant to be persistent entities, Play is merely acting as a stateless and persistent-less presentation layer for a restful API
The new Binder implementation of Play 1.2.4 allows this.
In Play versions earlier to 1.2.4 (released December 02, 2011) you could not bind a set by sending parameters like
The problem with binding to a List is that you will get a List of length 5 where the value at 0 and 2 is null.
If you still need to use Play version minor than 1.2.4, a workaround for this problem could be using
instead of Set or List. Then you won’t get null elements in between and you will get Collection by calling groups.values().