Play Framework Java - 表单验证错误

时间:2015-11-06 21:00:26

标签: java forms validation data-binding playframework

我正在尝试在Java Play中填写我的用户表单! 2.4.3,但我总是得到“IllegalStateException:No value”

这是Controller的代码:

Form<User> uf = Form.form(User.class);

uf.fill( new User( "felix@abc.com" , "123") );

if (uf.hasErrors()){
    return ok("Form Error");
}

// IllegalStateException: No value
uf.get();

User.class:

@Constraints.Required
private String email;
@Constraints.Required
private String password;

public User(){}

public User(String email, String password) {
    this.email = email;
    this.password = password;
}

//Getter
public String getEmail() {
    return email;
}

public String getPassword() {
    return password;
}

//Setter
public void setEmail(String email) {
    this.email = email;
}

public void setPassword(String password) {
    this.password = password;
}

我还尝试使用bind方法和HashMap填充Form,但得到相同的错误

Form<User> uf = Form.form(User.class);    

Map<String,String> data = new HashMap();
data.put("email", "felix@abc.com");
data.put("password", "123");

User user = uf.bind(data);

if (uf.hasErrors()){
    return ok("Form Error");
}

// IllegalStateException: No value
uf.get();

1 个答案:

答案 0 :(得分:0)

.fill方法返回一个新的Form对象(see here)。所以我相信这应该工作(没有测试):

Form<User> uf = Form.form(User.class);

Form<User> newUf = uf.fill( new User( "felix@abc.com" , "123") );

if (newUf.hasErrors()){
    return ok("Form Error");
}

// Should Work
newu=Uf.get();

似乎.bindas seen here)的故事也是一样的。