【发布时间】: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