简体   繁体   English

编译错误:在Scala中验证注册表单密码时发现类型不匹配

[英]Compilation error :type mismatch found when validating sign up form passwords in scala

I try to validate sign up form using Play framework 2.1.1 and scala. 我尝试使用Play框架2.1.1和scala验证注册表单。 I got 我有

Compilation error :type mismatch; 编译错误:类型不匹配; found : (String, String, String, String, String, String, String, Int) => models.Contact required: (String, String, String, String, String, (String, String), String, Int) => ? 找到了:(字符串,字符串,字符串,字符串,字符串,字符串,字符串,整数)=>模型需要联系:(字符串,字符串,字符串,字符串,字符串,((字符串,字符串),字符串,整数)=>

models.scala : models.scala:

package models

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

Application.scala : Application.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 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">
}

在此处输入图片说明

Contact.password is a String , but in mapping password field is a (String, String) . Contact.password是一个String ,但在mapping password字段中是(String, String) You can not use tuple instead of String parameter in Contact.apply . 您不能在Contact.apply使用元组代替String参数。

You should transform your Mapping[(String, String)] to Mapping[String] like this: 您应该像这样将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})

Ugly solution: 丑陋的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM