java 表单验证播放框架2.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9982916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form validation play framework 2.0
提问by John Rodrick
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", "[email protected]");
anyData.put("password", "secret");
Everything works and i retrieve the LoginForm object with the correct values. Am i supposed to catch the Exception? Shouldn't play take care of that and set loginForm.hasErrors = true?
一切正常,我用正确的值检索 LoginForm 对象。我应该捕捉异常吗?不应该玩照顾那个并设置 loginForm.hasErrors = true?
Thanks for any help!
谢谢你的帮助!
回答by Petter Kjelkenes
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
回答by Handerson
This seems to be a bug with Play 2.0 framework. I was able to replicate the same problem locally.
这似乎是 Play 2.0 框架的一个错误。我能够在本地复制相同的问题。
I opened a ticket https://play.lighthouseapp.com/projects/82401-play-20/tickets/313in case you want to follow up.
我开了一张票https://play.lighthouseapp.com/projects/82401-play-20/tickets/313以防你想跟进。