[英]Play Framework Java - Form validation error
I'm trying to fill my User Form in Java Play! 我正在尝试在Java Play中填写用户表格! 2.4.3, but i always get a "IllegalStateException: No value" 2.4.3,但我始终会收到“ IllegalStateException:无值”
This is the code from the 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: 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;
}
I also tried to fill the Form with the bind Method and a HashMap but get the same Error 我也尝试用bind方法和HashMap填充表单,但得到相同的错误
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();
The .fill
method returns a new Form
object ( see here ). .fill
方法返回一个新的Form
对象( 请参见此处 )。 So I belive this should work (didnt test): 所以我相信这应该工作(同等测试):
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();
And it seems the story is also the same for .bind
( as seen here ). 而且似乎.bind
的故事也一样( 如此处所示 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.