【发布时间】:2014-02-02 01:45:28
【问题描述】:
我目前正在看剧! Java 框架,我遇到了一个非常奇怪的错误:
我有一个包含以下必填字段的模型(除了 id 没有其他字段)
@Lob
@Constraints.Required
private String content;
@Constraints.Email
@Constraints.Required
private String email;
@Constraints.Required
private String title;
我的控制器中有以下方法:
public static Result createEntry() {
Form<BlogEntry> filledForm = blogEntryForm.bindFromRequest();
if (filledForm.hasErrors()) {
Logger.debug(filledForm.data().toString());
Logger.debug(filledForm.errors().toString());
return badRequest(newentry.render(filledForm));
}
BlogEntry entry = filledForm.get();
entry.save();
return redirect(routes.BlogController.index());
}
public static Result newEntry() {
return ok(newentry.render(blogEntryForm));
}
视图看起来像这样:
@(blogform: Form[BlogEntry])
@import helper._
@main("New Blog Entry") {
@form(routes.BlogController.createEntry()) {
@if(blogform.hasErrors) {
Errors in form
}
<fieldset>
<div>
@inputText(blogform("email"), '_label -> "Email")
</div>
<div>
@inputText(blogform("title"), '_label -> "Title")
</div>
<div>
@inputText(blogform("content"), '_label -> "Content")
</div>
<button type="submit">Submit</button>
</fieldset>
}
}
现在,当我在浏览器中导航到表单并输入一些数据,然后单击“提交”时,我被重定向到表单,因此调用了代码的 badRequest 部分。所以我开始记录表单和验证错误的输出,结果如下:
[调试] 应用程序 - {content=test, title=test, email=me@example.com}
[调试] 应用程序 - {content=[ValidationError(content,error.required,[])], title=[ValidationError(title,error.required,[])], email=[ValidationError(email,error.必填,[])]}
数据肯定在那里,当我在提交后被重定向到表单时,字段仍然填充了正确的数据。我在这里遗漏了什么明显的东西吗?
【问题讨论】:
标签: java forms validation playframework