简体   繁体   English

Play Framework Form验证错误

[英]Play Framework Form validation error

I'm currently looking at the Play! 我正在看Play! Framework for Java, and I've run into a pretty curious error: Java的框架,我遇到了一个非常奇怪的错误:

I've got a model with the following required fields (no other fields except an id) 我有一个带有以下必填字段的模型(除了id之外没有其他字段)

@Lob
@Constraints.Required
private String content;

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

@Constraints.Required
private String title;

I've got the following methods in my 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));
}

And the view looks like this: 视图看起来像这样:

@(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>
    }
}

Now when I navigate to the form in my browser, and enter some data, then click "submit", I get redirected to the form, so the badRequest portion of the code was called. 现在,当我在浏览器中导航到表单并输入一些数据时,单击“提交”,我被重定向到表单,因此调用了代码的badRequest部分。 So I started logging the output of both form and validation errors, and this came out: 所以我开始记录表单和验证错误的输出,这就出现了:

[debug] application - {content=test, title=test, email=me@example.com} [debug] application - {content = test,title = test,email=me@example.com}

[debug] application - {content=[ValidationError(content,error.required,[])], title=[ValidationError(title,error.required,[])], email=[ValidationError(email,error.required,[])]} [debug] application - {content = [ValidationError(content,error.required,[])],title = [ValidationError(title,error.required,[])],email = [ValidationError(email,error.required,[ ])]}

The data is definitely there, and when I'm redirected to the form after submitting, the fields are still filled with the correct data. 数据肯定存在,当我在提交后重定向到表单时,字段仍然填充正确的数据。 Am I missing something obvious here? 我错过了一些明显的东西吗?

Figured this out now: The reason was that I had no setters for the fields in my model. 现在想出来:原因是我的模型中的字段没有设置器。 That way the form was unable to set the properties and failed silently. 这样,表单无法设置属性并以静默方式失败。

Hello I know it has been a year since you've looked at this but I have a little more information for anyone just coming to this now. 你好,我知道你已经看了一年已经有一年了,但我现在有更多的信息可供刚刚来这里的人使用。 In using 2.4.x and setting up ebeans I came across this page: Play Enhancer . 在使用2.4.x并设置ebeans时,我遇到了这个页面: Play Enhancer The play enhancer is byte code magic that allows the use of public fields and direct access in the project but when built actually encapsulates the fields. 播放增强器是字节代码魔术,它允许在项目中使用公共字段和直接访问,但在构建时实际封装了字段。

The enhancer looks for all fields on Java classes that: 增强器查找Java类上的所有字段:

  • are public 是公开的

  • are non static 是非静态的

  • are non final 不是最终的

For each of those fields, it will generate a getter and a setter if they don't already exist. 对于每个字段,如果它们尚不存在,它将生成一个getter和一个setter。 If you wish to provide a custom getter or setter for a field, this can be done by just writing it, the Play enhancer will simply skip the generation of the getter or setter if it already exists. 如果您希望为字段提供自定义getter或setter,只需编写它就可以完成,Play增强器只会跳过getter或setter的生成(如果已经存在)。

In using the ebeans ORM, the Play Enhancer gets turned on by default. 在使用ebeans ORM时,Play Enhancer默认打开。 This is from the default plugins.sbt file that comes with a Play! 这是来自Play附带的默认plugins.sbt文件! application: 应用:

// 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")

The options are to have public fields and use them without getters and let play alter it under the hood or use private fields and define your own getters and setters. 选项是拥有公共字段并在没有getter的情况下使用它们,让玩游戏更改它或使用私有字段并定义自己的getter和setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM