validation of fields in a form in scala with lift frame work -
i working lift framework , scala. have form sign application, , want validate fields in it. have snippet access form values, , 1 validation class wrote validation functions. following code i've tried far. in snippet:
if(validationclassobject.validatename(first_name)){ if(validationclassobject.validatename(last_name)){ if(validationclassobject.validateemail(email)){ if(validationclassobject.validateusername(name)){ // adding values db s.redirectto("/") } else{ s.notice("invalid user name") } } else{ s.notice("invalid mail id") } } else{ s.notice("invalid last name") } } else{ s.notice("invalid first name") }
in validationclass
wrote validation code looks like:
//function validating mail address def validateemail(email: string): boolean = """(\w+)@([\w\.]+)""".r.unapplyseq(email).isdefined //code validating remaining fileds above
this working, know not best way of coding operation in scala. how modify code in more scalable way? how can use case classes here?
you do:
def av[t,v](validationfunction: => boolean, error: => t)(f: => v)={ if(!validationfunction) error else f } def v[v](validationfunction: => boolean, error: => string)(f: => v)=av(validationfunction,s.notice(error))(f) import validationcalssobject._ v(validatename(last_name),"invalid last name"){v(validatename(name),"invalid user name"){...}}
av
abstract method t
, v
result types error
function , continue function f
. v
more specific function expects string error
, encapsulates notice()
call. give f
part in curly braces v(validation, errormsg){/*todo when there no problem*/}
.
Comments
Post a Comment