【问题标题】:Play framework 2.0 scala form error handlingPlay framework 2.0 scala 表单错误处理
【发布时间】:2012-10-18 07:12:57
【问题描述】:

我的表单有问题,问题是我无法正确显示错误(我认为是这样)。验证工作正常。

当旧密码不正确时,输入框下方会显示错误消息,而当密码不匹配时,错误不会出现在任何地方。

如果我进行一些调试,我会得到数据:

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

我明白了:

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

所以我的问题是如何在视图中修复表单或代码,以正确打印该错误。

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(("", ("", ""))))
         )

在我看来:

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

【问题讨论】:

  • 这可能是一个转储问题,但您如何在表单中获得user.id
  • 我使用来自github.com/t2v/play20-auth 的模块,并且有一个名为authorizedAction 的方法,它给了我一个用户。
  • 谢谢,我会检查的。

标签: forms scala playframework-2.0


【解决方案1】:

首先,您可能希望对密码字段使用辅助方法,因为它们有一个。看看views.html.helper.inputPassword 并像这样使用它:

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

Passwords don't match 错误没有显示的原因是,它是绑定到表单本身而不是特定字段的错误。您必须检查表单的errors 字段,这将为您提供Seq[FormError]。然后,您可以以适当的方式显示这些...

最好的问候

【讨论】:

  • 感谢帮助,但 inputPassword 的工作很奇怪,当密码更新不成功时,我得到了填充值的表单。我的解决方案是:@helper.input(pass_form("old_password"),'_error->pass_form("old_password").error)
猜你喜欢
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 2016-02-08
相关资源
最近更新 更多