简体   繁体   English

播放框架2.0 scala表单错误处理

[英]Play framework 2.0 scala form error handling

I have a problem with form , the problem is that I can't show error in view properly(I think so). 我的表单有问题,问题是我无法正确显示错误(我想是这样)。 Validation works fine. 验证工作正常。

When the old password is incorrect error message is displayed below input box on the other hand when passwords are mismatched error isn't appearing anywhere. 当旧密码不正确时,如果密码不匹配,则输入框下方会显示错误消息,错误不会出现在任何地方。

If I do some debug I get data: 如果我做一些调试,我得到数据:

from in view: @pass_form("password").errors 从视图中: @pass_form("password").errors

I get this: 我明白了:

FormError(password,Passwords dont match,WrappedArray()) 

So my question how to fix form or code in view, to print that error properly. 所以我的问题是如何修复视图中的表单或代码,以正确打印该错误。

Form(
    mapping(
        "old_password" -> text.verifying(Messages("forms.password.old.mismatch"),
        password => User.correct_?(user.id, password)),
       "password" -> tuple(
       "new" -> text(minLength = conf.getInt("password.length.min").get),
       "confirm" -> text).verifying(Messages("forms.password.new.mismatch"), 
          passwords => passwords._1 == passwords._2)
      )
    ((_, password) => password._1)((_) => Some(("", ("", ""))))
         )

In view I have: 鉴于我有:

@helper.form(action = routes.UserController.submitPassword) {

        @helper.input(pass_form("old_password")) { (id, name, value, args) =>
            <input type="password" name="@name" id="@id" @toHtmlArgs(args)>  
        }

        @helper.input(pass_form("password.new")) { (id, name, value, args) =>
            <input type="password" name="@name" id="@id" @toHtmlArgs(args)>  
        }

        @helper.input(pass_form("password.confirm")) { (id, name, value, args) =>
            <input type="password" name="@name" id="@id" @toHtmlArgs(args)>  
        }

        <input type="submit" value="Set">
    }

first of all you might want to use the helper method for your password fields, since there is one for them. 首先,您可能希望对密码字段使用辅助方法,因为它们有一个。 Have a look at views.html.helper.inputPassword and use it like so: 看看views.html.helper.inputPassword并像这样使用它:

@helper.inputPassword(field = pass_form("old_password"))

The reason why the Passwords don't match error is not displayed is, that it is an error bound to the form itself and not to a specific field. 不显示密码不匹配错误的原因是,它是绑定到表单本身而不是特定字段的错误。 You will have to check the errors field of your form for that, which will give you a Seq[FormError] . 您必须检查表单的errors字段,这将为您提供Seq[FormError] You can then display these in an appropriate manner... 然后,您可以以适当的方式显示这些......

Best regards 最好的祝福

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

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