我正在尝试为枚举字段创建@select输入。在提交表单之前,一切正常。它失败了一个奇怪的验证错误 - > “error.invalid”
这是我的代码
枚举类
package model;
...
public enum UserType {
UserType_Admin("Administrator"), UserType_Monitor("Monitor"), UserType_Audit("Audit");
private String desc;
private UserType(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return Messages.get(desc);
}
public String getLabel() {
return toString();
}
public String getKey() {
return super.toString();
}
public static Map<String, String> options() {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
for (UserType ut : UserType.values()) {
Integer o = ut.ordinal();
options.put(o.toString(), ut.desc);
}
return options;
}
}
我的实体
@Entity
public class User extends Model {
...
@Id
public Long userID;
public UserType user_type;
}
Scala模板
@form(routes.Users.save(userID)) {
@select(
userForm("user_type"),
options(model.UserType.options),
'_label -> Messages("UserType"), '_default -> Messages("choose_user_type"),
'_showConstraints -> true
)
}
在控制器上保存方法:
public static Result save(Long userID) {
Form<User> userForm = form(User.class).bindFromRequest();
if (userForm.hasErrors()) { <- here it says that has errors
return badRequest(useredit.render(new Session(session()), userID,
userForm, new User()));
}
...
}
如果我检查userForm变量,我得到:
Form(of = class model.User,data = {user_type = 0},value = None, 误差= {USER_TYPE = [ValidationError(USER_TYPE,error.invalid,[])]})
字段user_type具有正确的值,如果我选择第一个项目则为0,为第二个项目为1,等等。
任何人都有这方面的线索或解决方法?也许禁用此字段的验证? Tks家伙