I'm new to Play 2.3 and having a lot of trouble with forms and data binding. Here's a scenario that's costing me a lot of headaches:
I have a model like this f.e. (annotations left out):
public class User extends Model {
public Integer id;
public String name;
public String anyOtherField;
}
Now I have a form in a scala.view where I want to be able to change the user's name:
@(userForm: Form[User])
@helper.form(action = routes.UserController.save()) {
@helper.inputText(userForm("name"), '_label -> "Name")
}
I call this view in my Controller like this:
Form<User> userForm = Form.form(User.class).fill(myUser);
return ok(views.html.usermgmt.useredit.render(userForm));
When the user submits the form I'm back in my Controller like that:
public static Result save() {
Form userForm = Form.form(User.class).bindFromRequest();
User user = userForm.get()
}
However, and now comes my big surprise: The resulting User from userForm.get()
has no fields filled other than "name". No "id", no "anyOtherField". What is the point in having that data binding when the resulting object is totally useless for further processing? Am I missing something?
If I don't miss something then I have to write a lot of boilerplate code in order to do the databinding myself:
- Either manually assign the changed fields to the real "user" object (which I have to re-fetch within
save()
first) - Or include hidden fields for all the fields in the model. In that case I better not forget updating the form if I add another field to the model.
Please tell me I'm missing sth.!?