播放表单验证
Play form verification
如何在播放表单中添加验证?
下面是重置密码表单,需要用户输入两次密码。
@(tokenId: String, form: Form[ResetPassword])(implicit messages: play.api.i18n.Messages, request: RequestHeader)
@main("Reset Password") {
@helper.form(routes.Application.handleResetPassword(tokenId)) {
@helper.inputText(form("password1"))
@helper.inputText(form("password2"))
<button type="submit">Submit</button>
}
}
在上面的表格中,我想添加验证以检查 password1 和 password2 是否相同。
谢谢
比利
你可以这样做:
val userFormConstraintsAdHoc = Form(
mapping(
"password1" -> text,
"password2" -> text
)(UserData.apply)(UserData.unapply) verifying("Failed form constraints!", fields => fields match {
case userData => form.password1.equals(form.password2)
})
)
这只是未经测试的伪代码,查看 docs for that purpose
如何在播放表单中添加验证?
下面是重置密码表单,需要用户输入两次密码。
@(tokenId: String, form: Form[ResetPassword])(implicit messages: play.api.i18n.Messages, request: RequestHeader)
@main("Reset Password") {
@helper.form(routes.Application.handleResetPassword(tokenId)) {
@helper.inputText(form("password1"))
@helper.inputText(form("password2"))
<button type="submit">Submit</button>
}
}
在上面的表格中,我想添加验证以检查 password1 和 password2 是否相同。
谢谢 比利
你可以这样做:
val userFormConstraintsAdHoc = Form(
mapping(
"password1" -> text,
"password2" -> text
)(UserData.apply)(UserData.unapply) verifying("Failed form constraints!", fields => fields match {
case userData => form.password1.equals(form.password2)
})
)
这只是未经测试的伪代码,查看 docs for that purpose