[英]Form validation play framework 2.0
I'm following the tutorial at http://www.playframework.org/documentation/2.0/JavaForms 我正在关注http://www.playframework.org/documentation/2.0/JavaForms上的教程
I've created a class LoginForm.java (Instead of User.class from the example. Not a class for persisting, just a form values holder) 我已经创建了一个类LoginForm.java(而不是示例中的User.class。不是用于持久化的类,只是一个表单值持有者)
package domain;
import static play.data.validation.Constraints.*;
public class LoginForm {
@Required
public String email;
public String password;
}
And in my controller i do (as the example), but i set the values to empty Strings to try the @Required annotation. 在我的控制器中我做(作为示例),但我将值设置为空字符串以尝试@Required注释。
Form<LoginForm> loginForm = form(LoginForm.class);
Map<String,String> anyData = new HashMap();
anyData.put("email", "");
anyData.put("password", "");
//Faking a post
LoginForm postedLoginForm = loginForm.bind(anyData).get();
if(loginForm.hasErrors()) {
//Just for this test task, should have another error handling..
return ok("@Required annotation kicked in..");
} else {
return ok("Got form values, email: " + postedLoginForm.email + " password: " + postedLoginForm.password);
}
But at: 但在:
LoginForm postedLoginForm = loginForm.bind(anyData).get();
I get an Execution exception [[IllegalStateException: No value]] 我得到执行异常[[IllegalStateException:No value]]
So it never checks/comes to 所以它永远不会检查/来
if(loginForm.hasErrors())
Does anyone know why this is? 有人知道为什么吗? If i set the values as the example:
如果我将值设置为示例:
Map<String,String> anyData = new HashMap();
anyData.put("email", "bob@gmail.com");
anyData.put("password", "secret");
Everything works and i retrieve the LoginForm object with the correct values. 一切正常,我使用正确的值检索LoginForm对象。 Am i supposed to catch the Exception?
我应该抓住异常吗? Shouldn't play take care of that and set loginForm.hasErrors = true?
不应该玩,并设置loginForm.hasErrors = true?
Thanks for any help! 谢谢你的帮助!
This is expected behavior. 这是预期的行为。
Note that you must use .get() on form After check for errors. 请注意,必须在表单上使用.get()检查错误。
LoginForm preLoginForm = loginForm.bind(anyData);
if(loginForm.hasErrors()) {
//Just for this test task, should have another error handling..
return ok("@Required annotation kicked in..");
}
LoginForm postedLoginForm = preLoginForm.get();
// ... Now use postedLoginForm
This seems to be a bug with Play 2.0 framework. 这似乎是Play 2.0框架的一个错误。 I was able to replicate the same problem locally.
我能够在本地复制同样的问题。
I opened a ticket https://play.lighthouseapp.com/projects/82401-play-20/tickets/313 in case you want to follow up. 我打开了一张票https://play.lighthouseapp.com/projects/82401-play-20/tickets/313,以备你跟进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.