不显示 Scala Play Forms 的错误消息

Error message not displaying Scala Play Forms

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

这是我的表格

  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
)

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

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

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