Reputation: 545
I am using Play Framework 2.0 with Scala.
So I have an action that is for a post request. I want to do some validation on the input data, and if the input is not valid, redirect to some other controller action (for example back to the prev page and tell the user the input was invalid)
My code looks something like this:
if ( input.isNotValid )
Redirect( foo )
// ... more code
Redirect( bar )
So the validation is early on, there are lines of code after that, and at the very end of the action I redirect to a different page.
My problem is that even when the validation fails, the page is not redirecting to Foo. The code works if I do this instead:
if ( input.isNotValid )
Redirect( foo )
else
Redirect( bar )
Am I required to put all my Redirects and Oks at the end of the action?
I know in Ruby on Rails sometimes this happens, and a solution to that is to put "and return" after every redirect. Is there something I have to do in Play Framework too?
Upvotes: 1
Views: 2402
Reputation: 5224
Redirect doesn't return control to the calling method - your code will continue to execute. Re-arrange your code so that the Redirect for invalid data is the final expression for that flow, like so:
if ( input.isNotValid )
Redirect( foo )
else {
// ... more code
Redirect( bar )
}
Additionally, this can make your code clearer by showing the various branches it can take more clearly.
Upvotes: 4