Play form verification

好久不见. 提交于 2020-01-06 15:52:11

问题


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>

  }

}

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

Thanks Pari


回答1:


You could do:

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



来源:https://stackoverflow.com/questions/39203521/play-form-verification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!