播放表单验证 [英] Play form verification

查看:87
本文介绍了播放表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在播放表单中添加验证?

How to add validation in play form?

下面是重置密码表单,希望用户输入两次密码.

Below is reset password form which expects user to enter password twice.

@(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是否相同.

In above form, I would like to add validation that will check if password1 and password2 are same or not.

谢谢 帕里

推荐答案

您可以这样做:

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)
  })
)

这只是未经测试的伪代码,请查看文档以了解这个目的

This is just untested pseudo code, check out the docs for that purpose

这篇关于播放表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆