How do you update ManyToOne field in a form?

2019-06-13 19:44发布

问题:

The questions is all in the title :)

Using Play! Framework 2.0, I have a Model with a ManyToOne field to an other Model.

In the form, I show it with a Select id -> toString, but when I hit submit, I've got "Invalid value", because it expect an instance of that Model (I guess).

How can I do this?

Thanks for your help!

回答1:

Name of this select must be set as property.id

ie. if your model looks like

@Entity
public class Entry extends Model {

    @Id
    public Long id;

    public String title;

    @ManyToOne
    public User user;
}    

Your select should be writen like this:

@select(
    yourForm("user.id"),
    options(User.options),
    'id -> "user",
    '_label -> "Select user"
)