Chris Stewart
Chris Stewart

Reputation: 1689

Error message not displaying Scala Play Forms

I'm trying to get an error message to display on my form when two passwords do not match on a registration page.

Here is my form

  private val userRegistrationForm =
    Form(mapping("id" -> optional(of[Long]), "firstName" -> text, "lastName" -> text,
      "phoneNumber" -> nonEmptyText, "emailAddress" -> email,
      "passwords" -> tuple(
        "password" -> nonEmptyText(minLength = 7),
        "confirmPassword" -> nonEmptyText(minLength = 7)).verifying(
          "Passwords don't match", password => password._1 == password._2).transform[String](
          password => password._1,
          password => ("", "")
      ))(user.apply _)(user.unapply _))

and here is my controller where I am binding the form from a request

  def submit = Action { implicit request =>
    Logger.info("Submit method in Landing Page controller")

    policyHolderRegistrationForm.bindFromRequest.fold(
      formWithErrors => {
        BadRequest(views.html.masterpage(registrationPageTitle)(registrationPageMeta)
            (views.html.policyHolderRegistration(formWithErrors, "There was an error on your form")))
      }, policyHolder => {
        policyHolderDAOActor ! PolicyHolderDAO.Create(policyHolder)
        Ok(views.html.masterpage("Home")("Home page for SuredBits")(views.html.home()))
      })
  } 

and finally here is the template

@helper.inputText(field=policyHolderRegistrationForm("firstName"), 'id ->"firstName", 
'name->"emailAddress", 'placeHolder -> "First Name", '_label -> None, 'required -> "true", 
  '_showConstraints -> false
)

@helper.inputText(field=userRegistrationForm("lastName"), 'id ->"lastName", 
'name->"lastName", 'placeHolder -> "Last Name", '_label -> None, 'required -> "true", 
  '_showConstraints -> false
)

@helper.inputText(field=userRegistrationForm("phoneNumber"), 'id ->"phoneNumber", 
'name->"phoneNumber", 'placeHolder -> "Phone Number", '_label -> None, 'required -> "true", 
  '_showConstraints -> false
)

@helper.inputText(field=userRegistrationForm("emailAddress"), 'id ->"emailAddress", 
'name->"emailAddress", 'placeHolder -> "Email Address", '_label -> None, 'required -> "true",  
  '_showConstraints -> false
)


@helper.inputPassword(field=userRegistrationForm("passwords.password"), 'id ->"password", 
 'placeHolder -> "Password", 'pattern -> ".{7,}", 'title -> "7 character password at minimum",
 'required -> "true", '_label -> None, '_showConstraints -> true, '_errors -> true
)

@helper.inputPassword(field=userRegistrationForm("passwords.confirmPassword"), 'id ->"confirmPassword", 
'placeHolder -> "Confirm Password", '_label -> None, 'required -> "true", 
  '_showConstraints -> true
)

Upvotes: 1

Views: 697

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

The error is tied to the passwords field, and not either of the specific password fields, because that is the mapping you're calling verifying on. I'm not sure helper can help you here, because the error is tied to both fields.

You might have to deal with the Form object directly to do something with the error:

@userRegistrationForm.error("passwords").map { error =>
    error.message
}

Upvotes: 3

Related Questions