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

时间:2015-02-05 04:17:38

标签: scala playframework playframework-2.0 playframework-2.3

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

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

1 个答案:

答案 0 :(得分:1)

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

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