Scala Play Forms 允许无效的电子邮件

Scala Play Forms allowing invalid emails

我正在尝试验证电子邮件是否有效。这是我正在尝试做的事情:

  private val emailForm =
    Form(mapping("id" -> optional(of[Long]), "email" -> email)(Email.apply _)(Email.unapply _))


  def submit = Action { implicit request =>
    emailForm.bindFromRequest.fold(
      formWithErrors => { // this code is never hit, even if I leave the textbox blank
        Ok(views.html.masterpage("Home")(views.html.landingPage(formWithErrors, "There was an error on your form")))
      }, email => {

          emailDAO ! EmailDAO.Create(email)
        Ok(views.html.masterpage("Thank you")(views.html.thankyou("Thank you for subscribing to our mailing list!")))
      })
}

我相信...您需要在表单定义中添加验证。

private val emailForm = Form(
  mapping(
    "id" -> optional( of[ Long ] ),
    "email" -> email.verifying( emailAddress )
  )( Email.apply _ )( Email.unapply _ )
)