Play Framework FORM binding to a set instead of a list

Is there a way to properly bind to a set in a form?

I am doing a POJO binding and my controller accepts 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 make my field snap to groups

I tried user.groups []. id, user.groups [0] .id, user.groups.id. I can make it work with the list just fine, but when it posts, I get a list of N elements with a bunch of zero elements (one zero for each checkbox that was not checked), and I could just create a new list without zeros but it seems wrong.

Edit: The user and group are not intended for permanent objects, Play simply acts as a level of a stagnant and constant level for a calm API

+5
3

Binder Play 1.2.4 .

1.2.4 ( 02 2011 .) , ,

user.groups[1].id = 1
user.groups[1].name = "name1"
user.groups[3].id = 3
user.groups[3].name = "name3"
user.groups[4].id = 4
user.groups[4].name = "name4"

, 5, 0 2 null.

Play , , 1.2.4,

Map<String, Group> groups

Set List. , Collection, groups.values ​​().

+3

-, User Group Entity, play.db.jpa.Model, @Entity. :

@Entity
public class User extends Model {

    ...

}

Model (.. public Long id), DAO (. Javadoc)

User :

@ManyToMany
public Set<Group> groups;
0

Play. Play binding - , .

public void setGroups(List<Group> groups)

. , .

I also recommend that you create a Unit test case to simulate a binding. Create a map to simulate a POST request and map the map to your object in Binder. This is much easier than repeating the process from the web interface, and it generates a regression test and warns you if the Play update breaks some of the binding rules (this happens to me).

0
source

All Articles