Alan Coromano
Alan Coromano

Reputation: 26048

Flash doesn't show any message

I have a _flash subtemplate:

@(flash: Flash)

@flash.data.foreach { x =>
    @x._1, @x._2
}

This code never shows any flash message. The code below doesn't work either

@if(!flash.isEmpty) {
    @for((msgKey, msgValue) <- flash) { 
        <div class="alert alert-@msgKey" data-dismiss="alert">      
            <a title="@Messages("misc.message.close")" class="close">×</a>
            @msgKey
        </div>
    }
}

due to the error at runtime: filter is not a member of Flash. And oddly enough, the code below does work and show a message:

@flash.get("success")

How I set the message is not important because it works well in the last case, however, there is the code for it:

if (success) {
  Redirect(routes.Application.success).flashing("success" -> "Ok123")
} else {
    Ok(views.html.registration.add(newForm))
}

My aim is loop over flash because it want to show all messages in _flash subtemplate.

UPDATE: I realized why the first approach doesn't work (I have to use map instead). But why does the second one raise the error at runtime?

Upvotes: 0

Views: 193

Answers (1)

vitalii
vitalii

Reputation: 3365

Syntax error, you missed flash.data. From Play 2.0 API we see that Flash is a

case class Flash (data: Map[String, String]) extends Product with Serializable

So the correct code should be:

@if(!flash.isEmpty) {
    @for((msgKey, msgValue) <- flash.data) { 
        <div class="alert alert-@msgKey" data-dismiss="alert">      
            <a title="@Messages("misc.message.close")" class="close">×</a>
            @msgKey
        </div>
    }
}

I'm almost sure that this is compile-time error, you see it in runtime only because you use dev mode. BTW, I usually do something like this:

@()(implicit flash: play.api.mvc.Flash)

 @flash.get("success").map { message =>  
        <div class="alert alert-success alert-dismissable">
           <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
             @message
        </div>
    }

    @flash.get("failure").map { message =>
        <div class="alert alert-danger alert-dismissable">
           <a class="close" data-dismiss="alert">×</a> 
             @message
        </div>
    }  

Upvotes: 3

Related Questions