Reputation: 4031
I'm developing a play 2.2.3 application and I have some problems to bind the form to my model class. This is my model class:
package models;
import java.util.Date;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Access(AccessType.FIELD)
public class User {
@Id
private int id;
private String firstname;
private String lastname;
private Date birthdate;
private String gender;
private String username;
private String password;
// some getter and setters...
}
My form:
@(form: Form[User])
@helper.form(action = routes.Application.register) {
<fieldset>
<legend>Persönliche Daten</legend>
@helper.inputText(form("firstname"),'_label -> "Vorname:")
@helper.inputText(form("lastname"),'_label -> "Nachname:")
@helper.inputDate(form("birthdate"),'_label -> "Geburtstag:")
@helper.inputRadioGroup(form("gender"),options = Seq(("male"->"Männlich"),("female"->"Weiblich")),'_label -> "Geschlecht:")
</fieldset>
<fieldset>
<legend>Login Daten</legend>
@helper.inputText(form("username"),'_label -> "Benutzername*:")
@helper.inputText(form("password"),'_label -> "Passwort*:")
<input id="registersubmit" type="submit" value="Registrieren" accesskey="s"/>
<p id="requiredhint">Mit "*" gekennzeichnete Felder sind Pflichtfelder</p>
</fieldset>
}
And my action method:
@Transactional
public static Result register() {
Form<User> form = Form.form(User.class).bindFromRequest();
if (form.hasErrors()) {
return badRequest("/registration");
} else {
User user = form.get();
//JPA.em().persist(user);
System.out.println("User "+user.getUsername()+" successfully saved!");
return redirect("/index");
}
}
The fields are in the form, but they are afterwards not binded to the User and null is printed for user.getUsername(). What could be the problem?
Upvotes: 0
Views: 735
Reputation: 4031
@David, you were in the right direction. As I am mixing languages in the development phase, I had wrong names for the setters, which I would not think will be critical, but it was. So changing for example
public String getBenutzername() {
return username;
}
to
public String getUsername() {
return username;
}
fixed the problem.
Upvotes: 0
Reputation: 179
It seems to be fine. Can you put fields as public and remove getters and setters? Just to check if there is a problem with them or put the complete User class in the example.
Upvotes: 1