title: How to pass flash data from controller to view with Play! framework
body: <p>I've been plowing through play! so far with a few bumps in the learning curve. Right now I am unable to pass flash data from the controller to the view, which at first I thought was a trivial task, or at least it should be.</p>
  
  <p>Here's what I have right now:</p>
  
  <p>I have a main layout: application.scala.html</p>
  
  <p>I have a view that goes in the layout: login.scala.html</p>
  
  <p>and I have my controller and method: UX.authenticate() - I want this to provide flash data to the view depending on the outcome of the login attempt (successful vs fail)</p>
  
  <p>This is my code in my controller method:</p>
  
  <pre><code>def authenticate = Action { implicit request =&gt;
          val (email, password) = User.login.bindFromRequest.get
          // Validation
          // -- Make sure nothing is empty
          if(email.isEmpty || password.isEmpty) {
              flash + ("message" -&gt; "Fields cannot be empty") + ("state" -&gt; "error")
              Redirect(routes.UX.login())
          }
          // -- Make sure email address entered is a service email
          val domain = email.split("@")
          if(domain(1) != "example.com" || !"""(\w+)@([\w\.]+)""".r.unapplySeq(email).isDefined) {
              flash + ("message" -&gt; "You are not permitted to access this service") + ("state" -&gt; "error")
              Redirect(routes.UX.login())
          } else {
              // Attempt login
              if(AuthHelper.login(email, password)) {
                  // Login successful
                  val user = User.findByEmail(email)
                  flash + ("message" -&gt; "Login successful") + ("state" -&gt; "success")
                  Redirect(routes.UX.manager()).withSession(
                    session + (
                      "user"      -&gt; user.id.toString
                    )
                  )
              } else {
                  // Bad login
                  flash + ("message" -&gt; "Login failed") + ("state" -&gt; "error")
                  Redirect(routes.UX.login())
              }
          }
      }
  </code></pre>
  
  <p>In my login view I have a parameter: <code>@(implicit flash: Flash)</code></p>
  
  <p>When I try to use flash nothing appears using <code>@flash.get("message")</code></p>
  
  <p>Ideally I would want to set <code>@(implicit flash: Flash)</code> in the layout, so that I can flash data from any controller and it will reach my view. But whenever I do that, login view throws errors.</p>
  
  <p>In my login view right now I have this:</p>
  
  <pre><code>def login = Action { implicit request =&gt;
          flash + ("message" -&gt; "test")
          Ok(views.html.ux.login(flash))
      }
  </code></pre>
  
  <p>What is the ideal way of passing flash data to the view, and are there examples anywhere? The examples on the Play! framework docs do not help whatsoever and are limited to two examples that show no interaction with the view at all (found here at the bottom: <a href="http://www.playframework.com/documentation/2.0/ScalaSessionFlash">http://www.playframework.com/documentation/2.0/ScalaSessionFlash</a>).</p>
  
  <p>Is there an easier alternative? What am i doing wrong? How can I pass flash data directly to my layout view?</p>