Hi
If I have the following entities:
@Entity
public class Unit extends Model {
@Id
public Long id;
...
public static Map<String, String> getAllAsMap() {
... // Returns map with id as key
}
}
@Entity
public class User extends Model {
@Id
public Long id;
@ManyToMany
public Set<Unit> units;
...
}
and the following view:
@(userForm: Form[User], availableUnits: Map[String, String])
@main("Add User", "Users") {
@helper.form(action = routes.Users.create) {
<fieldset>
...
@helper.select(userForm("
units.id"), options(availableUnits), 'multiple -> "multiple", '_label -> "Unit", '_error -> userForm.error("
units.id"))
</fieldset>
...
}
}
and the following controller with methods to render and submit the form
public class Users extends Controller {
...
public static Result addForm() {
return ok(addFormView.render(form(User.class), Unit.getAllAsMap()));
}
public static Result create() {
Form<User> userForm = form(User.class).bindFromRequest();
validateForm(userForm);
if (userForm.hasErrors()) {
return badRequest(addFormView.render(userForm, Unit.getAllAsMap()));
} else {
userForm.get().save();
return redirect(controllers.routes.Users.list());
}
}
...
}
Is it possible to bind a multi select list automatically into a ManyToMany relationship from the request like this?
I have no problem binding to single string fields, single enum fields or ManyToOne fields.
br
P-A Söderqvist