Ram*_*sad 0 scala playframework playframework-2.0
我尝试使用Play framework 2.1.1和scala验证注册表单.我有
编译错误:类型不匹配; found:(String,String,String,String,String,String,String,Int)=> models.Contact required :( String,String,String,String,String,(String,String),String,Int)=>?
models.scala:
package models
case class Contact(firstname: String, lastname: String, jobtitle: String, phone:String, email: String, password: String, companyname: String, employeescount: Int)
Run Code Online (Sandbox Code Playgroud)
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)
)
Run Code Online (Sandbox Code Playgroud)
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">
}
Run Code Online (Sandbox Code Playgroud)

Contact.password是一个String,但在mapping password现场是一个(String, String).你不能使用元组而不是String参数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})
Run Code Online (Sandbox Code Playgroud)
难看的解决方案:
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))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1401 次 |
| 最近记录: |