【问题标题】:Error message not displaying Scala Play Forms错误消息不显示 Scala Play Forms
【发布时间】:2015-04-17 08:14:33
【问题描述】:

当注册页面上的两个密码不匹配时,我试图在我的表单上显示一条错误消息。

这是我的表格

  private val userRegistrationForm =
    Form(mapping("id" -> optional(of[Long]), "firstName" -> text, "lastName" -> text,
      "phoneNumber" -> nonEmptyText, "emailAddress" -> email,
      "passwords" -> tuple(
        "password" -> nonEmptyText(minLength = 7),
        "confirmPassword" -> nonEmptyText(minLength = 7)).verifying(
          "Passwords don't match", password => password._1 == password._2).transform[String](
          password => password._1,
          password => ("", "")
      ))(user.apply _)(user.unapply _))

这是我的控制器,我在其中绑定来自请求的表单

  def submit = Action { implicit request =>
    Logger.info("Submit method in Landing Page controller")

    policyHolderRegistrationForm.bindFromRequest.fold(
      formWithErrors => {
        BadRequest(views.html.masterpage(registrationPageTitle)(registrationPageMeta)
            (views.html.policyHolderRegistration(formWithErrors, "There was an error on your form")))
      }, policyHolder => {
        policyHolderDAOActor ! PolicyHolderDAO.Create(policyHolder)
        Ok(views.html.masterpage("Home")("Home page for SuredBits")(views.html.home()))
      })
  } 

最后是模板

@helper.inputText(field=policyHolderRegistrationForm("firstName"), 'id ->"firstName", 
'name->"emailAddress", 'placeHolder -> "First Name", '_label -> None, 'required -> "true", 
  '_showConstraints -> false
)

@helper.inputText(field=userRegistrationForm("lastName"), 'id ->"lastName", 
'name->"lastName", 'placeHolder -> "Last Name", '_label -> None, 'required -> "true", 
  '_showConstraints -> false
)

@helper.inputText(field=userRegistrationForm("phoneNumber"), 'id ->"phoneNumber", 
'name->"phoneNumber", 'placeHolder -> "Phone Number", '_label -> None, 'required -> "true", 
  '_showConstraints -> false
)

@helper.inputText(field=userRegistrationForm("emailAddress"), 'id ->"emailAddress", 
'name->"emailAddress", 'placeHolder -> "Email Address", '_label -> None, 'required -> "true",  
  '_showConstraints -> false
)


@helper.inputPassword(field=userRegistrationForm("passwords.password"), 'id ->"password", 
 'placeHolder -> "Password", 'pattern -> ".{7,}", 'title -> "7 character password at minimum",
 'required -> "true", '_label -> None, '_showConstraints -> true, '_errors -> true
)

@helper.inputPassword(field=userRegistrationForm("passwords.confirmPassword"), 'id ->"confirmPassword", 
'placeHolder -> "Confirm Password", '_label -> None, 'required -> "true", 
  '_showConstraints -> true
)

【问题讨论】:

  • verifying 放在整个表单上,并将生成的问题呈现在全局表单错误部分。

标签: scala playframework playframework-2.0 playframework-2.3


【解决方案1】:

该错误与passwords 字段相关,而不是任何一个特定的密码字段,因为那是您调用verifyingmapping。我不确定helper 能否在这里为您提供帮助,因为该错误与这两个字段有关。

您可能必须直接处理 Form 对象来处理错误:

@userRegistrationForm.error("passwords").map { error =>
    error.message
}

【讨论】:

  • 在玩 2.6 我使用类似的东西:@userRegistrationForm.error("passwords").map { error => @error.format }
猜你喜欢
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 2010-12-12
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多