自定义播放scala表单不显示验证错误

时间:2012-11-03 15:08:28

标签: forms scala playframework playframework-2.0

以下是模板文件的片段:

 @helper.form(routes.Signup.signupCont) {
           @helper.inputText(signupForm("teamname"))
                  <div class="row">
                      <div class="span2"><label class="label_form_email_signup">Team name</label></div>
                      <div class="span4"><input id="Signup_teamname" type="text" class="span4 input" maxlength="24" name="teamname"></div>
                  </div>
}

表单定义:

def signupForm = Form(
    mapping(
      "teamname" -> text(minLength = 4, maxLength = 30).verifying(
        "Name already in use.", teamname => Team.findByNameStrict(teamname).isEmpty),
      "email" -> email.verifying(
        "Email already in use.", email => Team.findByEmail(email).isEmpty).verifying(
        "University email required", email => email.matches(""".*\.ac\.uk""")),
      "password" -> tuple(
        "main" -> text(minLength = 6),
        "confirm" -> text).verifying(
          "Passwords do not match.", password => password._1 == password._2))((teamname, email, password) =>
        (teamname, email, password._1))(data => Some(data._1, data._2, ("",""))))

当使用错误的电子邮件并提交表单时,例如,页面重新加载,但表单字段为空(因此解构器失败),并且电子邮件框旁边没有错误消息。如何实现这种行为?

1 个答案:

答案 0 :(得分:1)

确保在渲染时将带有错误的表单传递给您的视图。例如:

signUpForm.bindFromRequest.fold (
  errors => {
    /// assuming the page is signUp
    BadRequest(views.html.signUp(errors))
  },
  info => {
    // all ok, process the form 
  }
 )