0

我尝试使用 Play framework 2.1.1 和 scala 验证注册表单。我有

编译错误:类型不匹配;找到:(字符串,字符串,字符串,字符串,字符串,字符串,字符串,整数)=> 模型。需要联系:(字符串,字符串,字符串,字符串,字符串,(字符串,字符串),字符串,整数)=>?

模型.scala:

package models

case class Contact(firstname: String, lastname: String, jobtitle: String, phone:String, email: String, password: String, companyname: String, employeescount: Int)

应用程序.scala:

 val contactForm = Form(
  mapping(      
    "firstname" -> nonEmptyText(minLength=2, maxLength=10),
    "lastname" -> nonEmptyText(minLength=2, maxLength=10),
    "jobtitle" -> nonEmptyText(minLength=2, maxLength=10),
    "phone" -> nonEmptyText(minLength=2, maxLength=10),      
    "email" -> (email verifying nonEmpty),

    "password" -> tuple(
        "main" -> text(minLength = 6),
        "confirm" -> text
    ).verifying(
        // Add an additional constraint: both passwords must match
        "Passwords don't match", { case (p1, p2) => p1 == p2 }
      ).transform({case (p, _) => p}, {p => p -> p}),

  "companyname" -> nonEmptyText,
    "employeescount" -> number
  )(Contact.apply)(Contact.unapply)
)

index.scala.html

@form(routes.Application.save()) {      
        @text(contactForm("firstname"), '_label -> "First Name : ", 'toto -> "titi")
        @text(contactForm("lastname"), '_label -> "Last Name : ", 'toto -> "titi")
        @text(contactForm("jobtitle"), '_label -> "Job Title : ", 'toto -> "titi")
        @text(contactForm("phone"), '_label -> "Phone : ", 'toto -> "titi")
        @text(contactForm("email"), '_label -> "Email : ")

        @password(contactForm("password.main"), '_label -> "Password : ")
        @password(contactForm("password.confirm"), '_label -> "Confirm Password : ")

        @text(contactForm("companyname"), '_label -> "Company Name : ", 'toto -> "titi")
        @select(
            contactForm("employeescount"), 
            options(
                "0" -> "0-10",
                "1" -> "10-100",
                "2" -> "100-1000",
                "2" -> " > 1000"
            ),
            '_label -> "Employees"
        )

        <input type="submit" value="Submit">
}

在此处输入图像描述

4

1 回答 1

5

Contact.password是 a String,但在mapping password字段中是 a (String, String)。您不能使用 tuple 代替String参数 in Contact.apply

你应该把你Mapping[(String, String)]的变成Mapping[String]这样:

"password" -> tuple(
    "main" -> text(minLength = 6),
    "confirm" -> text
).verifying(
  // Add an additional constraint: both passwords must match
  "Passwords don't match", ps => ps._1 == ps._2
).transform[String]({ps => ps._1}, {p => p -> p})

丑陋的解决方案:

mapping(
  ...
  "password" -> tuple(
      "main" -> text(minLength = 6),
      "confirm" -> text
  ).verifying(
    // Add an additional constraint: both passwords must match
    "Passwords don't match", ps => ps._1 == ps._2
  )
  ...
){
  (firstname: String, lastname: String, jobtitle: String, phone:String, email: String, passwords: (String, String), companyname: String, employeescount: Int) =>
    Contact(firstname, lastname, jobtitle, phone, email, passwords._1, companyname, employeescount)
}{
  (c: Contact) => Option((c.firstname, c.lastname, c.jobtitle, c.phone, c.email, c.password -> c.password, c.companyname, c.employeescount))
}
于 2013-08-01T07:54:25.990 回答