EDIT: The form should not include all the classpath to the enum, just the enum name. So on the ContactType class, the put method has to be replaced to this:
put(value.name(),name);
I'm not able to bind the enum to the class. In the controller I have something like that:
public Result saveUser() {
Form<User> userForm = form(User.class).bindFromRequest();
if(userForm.hasErrors()) {
return badRequest(createUser.render(userForm));
}
User user=userForm.get();
user.save();
Logger.info(user.contacts.toString());
flash("success", "User " + userForm.get().identity + " has been created");
return GO_ADMIN;
}
Which is returning me all the time "error.invalid".
The POST response looks like that:
identity=asas&
status=1&
contacts[0].name=fasddf&
contacts[0].prename=afadfs&
contacts[0].email=ffdf@aa.com&
contacts[0].contactType=models.constats.ContactType.MANAGER
Just to have more information, I provide here the User and the Contact classes
User
public class User extends Model{
@Constraints.Required(message = "Field is required")
public String identity;
public Integer status;
@Valid
public List<Contact> contacts;
}
Contact
public class Contact extends Generic{
@Constraints.Required(message = "Field is required")
public String name;
@Constraints.Required(message = "Field is required")
public String prename;
@Constraints.Required(message = "Field is required")
@Constraints.Email
public String email;
public ContactType contactType;
}
ContactType
public enum ContactType {
ADMIN,SUPPORT,MANAGER,DEVELOPER,DESIGNER,NO_INFORMATION;
}
public static final Map<String,Object> types=new HashMap<String,Object>()
{{
for (ContactType value : ContactType.values()) {
String name=value.name().toLowerCase();
name=Character.toUpperCase(name.charAt(0)) + name.substring(1).replaceAll("_", " ");
put(ContactType.class.getName() + "."+ value.name(),name);
}
}};