Scala/Play Framework: How to modify fields in Form object before validation -
Scala/Play Framework: How to modify fields in Form object before validation -
one of simples things in web app should read fields html forms. play framework offers form class validate , map answers. works fine long don't want special things.
i found out with
val newform = user.copy(errors = user.errors++seq(formerror("email", "already registered")), info = user.data + ("username"->"correctedname")
i can modify errors , fields before redisplaying form.
but how possible modify fields before validated? nice remove unwanted white spaces, transform "11.05.2014" "11-may-2014" , such things.
you might able within form
object if create validation rules more lax (nonemptytext
instead of email
email unwanted whitespace), utilize transform
clean input, verifying
implement actual validation rule. end beingness ugly, won't go on next path.
i think you're alternative may passing form info through filter before calling bindfromrequest
. assuming you're using parse.urlformencoded
bodyparser
(which should be), request.data
map[string, seq[string]]
.
this rough illustration of filter, take map[string, seq[string]]
, , homecoming new map[string, seq[string]]
cleansed data.
def cleanuserform(data: map[string, seq[string]]): map[string, seq[string]] = { data.map{ case (key, values) => if(key == "email") (key, values.map(_.replaceall(" ", ""))) // trim whitespace email else (key, values) } }
then in controller, you'd have explicitly pass cleansed info bindfromrequest
(which accepts implicit request):
def register = action(parse.urlformencoded) { request => val incomingdata = request.body val cleandata = cleanuserform(incomingdata) userform.bindfromrequest(cleandata).fold( formwitherrors => ... user => ... ) } val userform: form[user] = form { ... }
forms scala validation playframework playframework-2.0
Comments
Post a Comment