我定义了一个案例类和一个表单对象来处理表单提交。
case class NewUser(
userName: String,
password: String,
email: String
)
val form = Form(
mapping(
"userName" -> nonEmptyText.verifying("The username already exists", User.userNameExists(_) == false),
"password" -> tuple(
"main" -> nonEmptyText,
"confirm" -> nonEmptyText
).verifying(
"Password does not match confirmed password", password => password._1 == password._2
).transform[String](
password => password._1,
password => ("", "")
),
"email" -> email.verifying("This email belongs to an existing account. Please try another one", emailExists(_) == false))
(NewUser.apply)(NewUser.unapply)
)
在模板中,代码为:
@helper.inputText(newUserForm("userName"), 'class -> "form-control", 'required -> "true")
@helper.inputPassword(newUserForm("password.main"), 'class -> "form-control", 'required -> "true")
@helper.inputPassword(newUserForm("password.confirm"), 'class -> "form-control", 'required -> "true")
@helper.inputText(newUserForm("email"), 'class -> "form-control")
如果用户键入现有的userName,则错误将以内联方式显示。但是,如果用户键入的confirm password
与password
不同,则错误不会显示在任何位置。
如何显示错误内联?任何人都可以帮忙吗?谢谢。