【问题标题】:Play Framework Form validation error播放框架表单验证错误
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 哇,是的,我的朋友在他的 repo 中有相同的代码,没有 getter 和 setter,它正在工作。但他使用的是 Java 8,而我使用的是 7。不知道这是否与它有关。
    【解决方案2】:

    您好,我知道您已经有一年没有看到这个了,但我有一些更多的信息给现在刚刚来这里的任何人。在使用 2.4.x 和设置 ebeans 时,我遇到了这个页面:Play Enhancer。播放增强器是字节码魔术,它允许在项目中使用公共字段和直接访问,但在构建时实际上封装了字段。

    增强器查找 Java 类上的所有字段:

    • 公开

    • 是非静态的

    • 不是最终的

    对于这些字段中的每一个,如果它们不存在,它将生成一个 getter 和一个 setter。如果您希望为字段提供自定义 getter 或 setter,只需编写它即可完成,如果 getter 或 setter 已经存在,Play 增强器将直接跳过它的生成。

    在使用 ebeans ORM 时,Play Enhancer 默认开启。这是来自 Play 附带的默认 plugins.sbt 文件!应用:

    // Play enhancer - this automatically generates getters/setters for public fields
    // and rewrites accessors of these fields to use the getters/setters. Remove this
    // plugin if you prefer not to have this feature, or disable on a per project
    // basis using disablePlugins(PlayEnhancer) in your build.sbt
    addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")
    
    // Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using
    // enablePlugins(SbtEbean). Note, uncommenting this line will automatically bring in
    // Play enhancer, regardless of whether the line above is commented out or not.
    addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")
    

    选项是拥有公共字段并在没有 getter 的情况下使用它们,让 play 在后台更改它或使用私有字段并定义自己的 getter 和 setter。

    【讨论】:

      猜你喜欢
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 2014-05-30
      相关资源
      最近更新 更多