简体   繁体   English

在Play上显示错误! 2框架

[英]Displaying error on Play! 2 Framework

I am having big difficulties following Play! Play之后我遇到了很大困难! 2.2.x documentation and i am currently stuck on how to display error from my form validation. 2.2.x文档,我目前仍然坚持如何从表单验证中显示错误。

This is my code: 这是我的代码:

Route 路线

GET         /account/create            controllers.Account.create()
POST        /account/create            controllers.Account.createAccount()

Model 模型

public static UserAccount create(UserAccount data){
    UserAccount account = data;
    String salt     = BCrypt.gensalt();

    account.email   = data.email;
    account.salt    = salt;
    account.hash    = BCrypt.hashpw(data.hash, salt);

    account.save();
    return account;
}

Controller 调节器

// handles POST method
public static Result createAccount(){
    Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
    if(userForm.hasErrors()){
        return badRequest();
    }else{
        UserAccount.create(userForm.get());
        Logger.info("Username is: " + userForm.get().email);
        return ok("ok, I recived POST data. That's all...");
    }
}

// Handles GET method
public static Result create(){
    return ok(
            views.html.account.form.render()
    );
}

Views 查看

@if(form.hasGlobalErrors) {
    <p class="error">
    @form.globalError.message
    </p>
}

@helper.form(action = routes.Account.createAccount()) {
    <input type="text" name="email" placeholder="Your Email Address"/><br/>
    <input type="password" name="password" placeholder="Your Password"/><br/>

    <input type="text" name="fname" placeholder="Your First Name"/><br/>
    <input type="text" name="midname" placeholder="Your Middle Name"/><br/>
    <input type="text" name="lname" placeholder="Your Last Name"/><br/>

    <input type="text" name="dob" placeholder="Your Birthday"/><br/>
    <select name="gender" id="gender">
        <option value="1">Male</option>
        <option value="2">Female</option>
        <option value="3">Undecided</option>
    </select><br/>

    <input type="submit" value="Login" />
}

Error Message 错误信息

value hasGlobalErrors is not a member of object views.html.account.form

Can anyone tell me what's wrong with my code? 谁能告诉我我的代码有什么问题? I am frustrated with the given example. 我对给定的例子感到沮丧。

EDIT #1: 编辑#1:

This is what i have done so far: 这是我到目前为止所做的:

Models: 楷模:

public static UserAccount create(UserAccount data){
    UserAccount account = data;
    String salt     = BCrypt.gensalt();

    account.email   = data.email;
    account.salt    = salt;
    account.hash    = BCrypt.hashpw(data.hash, salt);

    account.save();
    return account;
}

Controllers: 控制器:

// HANDLES GET REQUEST
public static Result create(){
    return ok(
            views.html.account.form.render(userForm)
    );
}

// HANDLES POST REQUEST
public static Result createAccount(){
    Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
    if(userForm.hasErrors()){
        return badRequest(views.html.account.form.render(userForm));
    }else{
        // UserAccount.create(userForm.get());
        // Logger.info("Username is: " + userForm.get().email);
        UserAccount data = userForm.get();
        return ok(data.email);
    }
}

VIEWS/TEMPLATE 查看/模板

@(form: Form[UserAccount])

@if(form.hasGlobalErrors) {
    <h1>Please fix the following error first</h1>
    <p>
        @form.globalError.message
    </p>
    <ul>
    @for(error <- form.globalErrors) {
        <li>@error.message</li>
    }
    </ul>
}

@helper.form(action = routes.Account.createAccount()) {
    <input type="text" name="email" placeholder="Your Email Address"/><br/>
    <input type="password" name="password" placeholder="Your Password"/><br/>

    <input type="text" name="fname" placeholder="Your First Name"/><br/>
    <input type="text" name="midname" placeholder="Your Middle Name"/><br/>
    <input type="text" name="lname" placeholder="Your Last Name"/><br/>

    <input type="text" name="dob" placeholder="Your Birthday"/><br/>
    <select name="gender" id="gender">
        <option value="1">Male</option>
        <option value="2">Female</option>
        <option value="3">Undecided</option>
    </select><br/>

    <input type="submit" value="Login" />
}

So far, according to firebug when i deliberately put errors on the form the server will return badrequest. 到目前为止,根据firebug,当我故意在表单上放错误时,服务器将返回badrequest。 However, no error is displayed by the template. 但是,模板不会显示错误。

If i change the controller as such: 如果我改变控制器:

public static Result createAccount(){
    Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
    if(userForm.hasErrors()){
        return ok(userForm.errorsAsJson().toString());
    }else{
        // UserAccount.create(userForm.get());
        // Logger.info("Username is: " + userForm.get().email);
        UserAccount data = userForm.get();
        return ok("ok, I received POST data. That's all...");
    }
}

Or if i do this on my View/Template 或者,如果我在我的视图/模板上执行此操作

<pre>@form.errorsAsJson.toString()</pre>

It works, and errors are printed accordingly. 它工作正常,并相应地打印错误。 Do anyone know what i am missing here? 有谁知道我在这里缺少什么?

EDIT #2: 编辑#2:

The best thing that works for me to output the error is by doing this on my View/Template 对我来说输出错误最好的办法是在我的视图/模板上执行此操作

@(form: Form[UserAccount])

@if(form.hasErrors) {
    <h1>Please fix the following error first</h1>

    <ul>
    @for(error <- form.errors) {
        <li>@error.toString</li>
    }
    </ul>
}

Which outputs this: 哪个输出:

(email,[ValidationError(email,error.required,[])])
(hash,[ValidationError(hash,error.required,[])])

Since I am trying to display an appropriate message to user, the message is rather useless. 由于我试图向用户显示适当的消息,因此该消息相当无用。

You can use the flash scope: 您可以使用闪存范围:

Controller: 控制器:

public Result create(){
   flash("error", "error msg");
   return badRequest(view.render());
}

View: 视图:

@flash.get("error")

The problem is that your form doesn't have "global errors", it has errors for the fields email and hash. 问题是您的表单没有“全局错误”,它有字段电子邮件和哈希的错误。 It's different. 这不一样。

You can check the error for the fields using: 您可以使用以下命令检查字段的错误:

@if(form.error("email") != null) { }

And you can fire global error as belor on controller: 你可以在控制器上发出全局错误:

form.reject("global error");

More info: http://www.playframework.com/documentation/2.0/JavaSessionFlash 更多信息: http//www.playframework.com/documentation/2.0/JavaSessionFlash

After looking deeper at your code, there are two things to note: 深入研究您的代码后,有两点需要注意:

  1. In your template you need to use the helper tags for your form fields. 在模板中,您需要使用表单字段的帮助程序标记。 The helpers will display field specific errors for you: http://www.playframework.com/documentation/2.2.x/JavaFormHelpers 帮助程序将为您显示特定于字段的错误: http//www.playframework.com/documentation/2.2.x/JavaFormHelpers

  2. I don't think your form has any global errors, which is why that code isn't displaying anything. 我不认为您的表单有任何全局错误,这就是为什么该代码不显示任何内容。

Try the helpers and see if that works for you. 尝试帮助,看看它是否适合你。

I need to see your full template, controller, and routes file. 我需要查看完整的模板,控制器和路由文件。 I think the problem is that you don't have a method signature at the top of your template file, something like this: 我认为问题是你的模板文件顶部没有方法签名,如下所示:

@(userForm: Form[User])

Once you fix the template, you have another problem that you are not passing the form to the view template in the "badRequest()" flow. 修复模板后,您还有另一个问题,即您没有将表单传递给“badRequest()”流程中的视图模板。 Try changing your controller action to look like this: 尝试将控制器操作更改为如下所示:

public static Result createAccount(){
    Form<UserAccount> userForm = form(UserAccount.class).bindFromRequest();
    if(userForm.hasErrors()){
        return badRequest(views.html.createUserTemplate.render(userForm));
    }else{
        UserAccount.create(userForm.get());
        Logger.info("Username is: " + userForm.get().email);
        return ok("ok, I recived POST data. That's all...");
    }
}

Your global errors code is mostly correct, it should look like this: 您的全局错误代码大多是正确的,它应如下所示:

@if(userForm.hasGlobalErrors) {
  <ul>
  @for(error <- userForm.globalErrors) {
    <li>@error.message</li>
  }
  </ul>
}

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

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