Created
January 30, 2013 19:08
-
-
Save bmc/4675829 to your computer and use it in GitHub Desktop.
Flash stuff, in Play.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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