2015-11-06 91 views
0

我想在Java Play中填充我的用户表单! 2.4.3,但我总是得到一个 “IllegalStateException异常:没有价值”玩框架Java - 表单验证错误

这是从控制器代码:

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

uf.fill(new User("[email protected]" , "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; 
} 

我也试图填补带有绑定方法和HashMap的表单但获取相同的错误

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

Map<String,String> data = new HashMap(); 
data.put("email", "[email protected]"); 
data.put("password", "123"); 

User user = uf.bind(data); 

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

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

回答

0

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

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

Form<User> newUf = uf.fill(new User("[email protected]" , "123")); 

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

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

而且似乎这个故事也是.bindas seen here)相同。