Validate scala forms, when form contains inner case classes -


help scala forms validation,

here case class form data:

  case class data(     firstname: string,     lastname: string,     email: string,     confirm_email: string,     password: string,     confirm_password: string) } 

and scala form:

  val form = form(     mapping(       "firstname" -> nonemptytext,       "lastname" -> nonemptytext,       "email" -> email,       "confirm_email" -> email,       "password" -> nonemptytext(minlength = 8),       "confirm_password" -> nonemptytext(minlength = 8))(data.apply)(data.unapply)) 

now problem need validate "email" , "confirm" email, problem need create tuples or mapping. , best way handle these kinds of form validation situations. can done using tuples , not mapping case class.

but can done if requried use mapping , case classes in forms.

first things first, i'd rid of confirm_email , confirm_password fields since they're redundant in data model. after operation, it'll this:

case class data(   firstname: string,   lastname: string,   email: string,   password: string) 

next, form mapping needs updated:

val form = form[data](   mapping(     "firstname" -> nonemptytext,     "lastname" -> nonemptytext,     "email" -> tuple(       "email1" -> email,       "email2" -> email     ).verifying(messages("form.error.emailnotequals"), email => email._1 == email._2),     "password" -> tuple(       "pass1" -> nonemptytext(minlength = 8),       "pass2" -> nonemptytext(minlength = 8)     ).verifying(messages("form.error.passwordnotequals"), password => password._1 == password._2)   )((firstname, lastname, email, password) => data(firstname, lastname, email._1, password._1))    ((form: data) => some((form.firstname, form.lastname, (form.email, form.email), ("", "")))) ) 

two changes required:

  • nested mapping validation both email , password fields.
  • custom apply , unapply method implementation in order map form 6 fields models case class 4 fields.

notice custom unapply method doesn't set values password fields since it's desired behaviour in virtually cases.

finally, view must altered refer new form tuple mapping correctly. instance, fields email should follows:

@helper.inputtext(dataform("email.email1")) @helper.inputtext(dataform("email.email2")) 

fields don't use new tuple mappings stay unchanged.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -