当注册页面上的两个密码不匹配时,我试图在表单上显示错误消息。
这是我的表格
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
)
答案 0 :(得分:3)
错误与passwords
字段相关联,而不是与任何特定密码字段相关联,因为这是您mapping
正在调用的verifying
。我不确定helper
可以在这里帮助你,因为错误与这两个字段有关。
您可能必须直接处理Form
对象以执行错误:
@userRegistrationForm.error("passwords").map { error =>
error.message
}