gpt4 book ai didi

java - Play Framework 表单验证错误

转载 作者:行者123 更新时间:2023-11-29 05:29:52 25 4
gpt4 key购买 nike

我目前正在看 Play! Java 框架,我遇到了一个非常奇怪的错误:

我有一个包含以下必填字段的模型(除 id 外没有其他字段)

@Lob
@Constraints.Required
private String content;

@Constraints.Email
@Constraints.Required
private String email;

@Constraints.Required
private String title;

我的 Controller 中有以下方法:

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));
}

View 看起来像这样:

@(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.必填,[])]}

数据肯定在那里,当我在提交后被重定向到表单时,字段仍然填充了正确的数据。我在这里遗漏了什么明显的东西吗?

最佳答案

现在想通了:原因是我的模型中的字段没有 setter 。这样表单就无法设置属性并默默地失败。

关于java - Play Framework 表单验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21506072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com