Skip to content

Instantly share code, notes, and snippets.

@bmc
Created January 30, 2013 19:08
Show Gist options
  • Save bmc/4675829 to your computer and use it in GitHub Desktop.
Save bmc/4675829 to your computer and use it in GitHub Desktop.
Flash stuff, in Play.
object SomeController extends Controller {
def signup = Action(parse.urlFormEncoded) { implicit request =>
signupForm.bindFromRequest.fold(
{ form => // failure; repost
BadRequest(views.html.register.signup(form))
},
{ newUser => // success; create user and send confirmation
val token = RandUtil.generateUniqueToken(newUser.email)
val user = newUser.toUser.copy(confirmationToken = Some(token))
User.create(user).fold(
{ error =>
// Can't use "flashing" here, because the template will already
// have been rendered by the time Ok is called. Instead, create
// our own flash object and pass it to the template.
//
// This COULD be done with an implicit parameter, but using an
// implicit parameter leads to less obvious code. Here's how,
// though:
//
// implicit val flash = Flash(Map(...))
// Ok(views.html.admin.edituser(...))
val flash = Flash(Map("error" -> error))
val filledForm = signupForm.fill(newUser)
Ok(views.html.register.signup(filledForm)(flash))
},
{ user =>
sendConfirmationEmail(user, request)
Redirect(routes.Register.postSignUp(token))
}
)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment