Jagveer Singh
Jagveer Singh

Reputation: 584

how to map form having more attributes than case class in play 2 with 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

My case class is -

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

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

Upvotes: 0

Views: 153

Answers (1)

Shrey
Shrey

Reputation: 2404

You need to remove the _ while binding the form.

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

Upvotes: 1

Related Questions