如何使用scala映射具有比案例类更多的属性的表单 [英] how to map form having more attributes than case class in play 2 with scala

查看:74
本文介绍了如何使用scala映射具有比案例类更多的属性的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

val registrationForm: Form[Registration]=Form(
mapping(
"fname"->text(minLength=2),
"lname"->text(minLength=1),
"userEmail"->text(minLength=5),
"userPassword" -> tuple(
"main" -> text(minLength = 6),
"confirm" -> text
).verifying(
// Add an additional constraint: both passwords must match
"Passwords don't match", userPassword => userPassword._1 == userPassword._2
),
"gender"->number,
"year"->number, 
"month"->number, 
"day"->number
)
{
// Binding: Create a User from the mapping result (ignore the second password and the 
accept field)
(fname, lname, userEmail, userPassword, gender, year, month, day, _) => 
Registration(fname,lname,userEmail,userPassword._1, gender, year,month,day)//error here
} 
{
// Unbinding: Create the mapping values from an existing User value
user => Some(user.fname, user.lname, user.userEmail,(user.userPassword, ""),
user.gender, user.year, user.month, user.day, false)
}

)//end registrationForm

我的案例类是 -

case class Registration(fname:String, lname:String, userEmail:String,
userPassword:String, gender:Int, year:Int,month:Int, day:Int )

上面的代码给出了一个错误——参数个数错误;预期 = 8.我对发生错误的行进行了评论

The above code is giving a error- wrong number of parameters; expected = 8. I have giving comment to the line in which error occurs

推荐答案

绑定表单时需要去掉_.

// Binding: Create a User from the mapping result 
//   (ignore the second password and the accept field)

(fname, lname, userEmail, userPassword, gender, year, month, day) =>
Registration(fname, lname, userEmail, userPassword._1, gender, year, month, day)

此外,您可能不想在取消绑定表单时输入密码.

Also, you may not want to put the password while unbinding the form.

// Unbinding: Create the mapping values from an existing User value

user => Some(user.fname, user.lname, user.userEmail, ("", ""),
            user.gender, user.year, user.month, user.day)

这篇关于如何使用scala映射具有比案例类更多的属性的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆