0
votes

I am currently struggling to find a way to implement alerts in a reusable and clean fashion. I am not sure if Play's flashing is only intended for redirects, but this post indicates that there's a workaround for flashing with instances of class Result.

The workaround suggests to use an implicit parameter for the view, for example:

@(clientForm: Form[models.Client])(implicit flash: Flash)

And then simply do something like this:

val flash = play.api.mvc.Flash(Map("error" -> "Please select another id for this client"))
Ok(views.html.clients.new_client(boundForm)(flash))

The problem is that I do have multiple implicit parameters in my view, i.d.:

(implicit request: RequestHeader, messages: Messages, flash: Flash)

So the compiler complains with:

Unspecified value parameters: flash: Flash, messages: Messages

How can I solve this?

Making flash implicit, as suggested in the (deleted) answer, didn't solve the problem. I still get this compile error:

ambiguous implicit values: both method request2flash in trait Controller of type (implicit request: play.api.mvc.RequestHeader)play.api.mvc.Flash and value flash of type play.api.mvc.Flash match expected type play.api.mvc.Flash

1
You don't need to pass the flash implicit parameter since there is an implicit conversion happening from request to flash via request2flash method as shown in the new error.Samar
For messages error, try import play.api.i18n.Messages.Implicits._Samar
Thanks for the help @Samar, but when making flash implicit and not passing it to the view, I still get this compile error. I don't think that it has something to do with Messages, since everything worked before introducing Flash. Do you have any other suggestions how alerts can be implemented in a reusable way?John Doe
dont make flash implicit. the implicit is automatically passed in. I mean remove this line implicit val flash = play.api.mvc.Flash(Map("error" -> "Please select another id for this client"))Samar
Okay thanks for clarifying, but then how do I get the flash object into the view?John Doe

1 Answers

0
votes

You don't need to pass the flash implicit parameter since there is an implicit conversion happening from request to flash via request2flash method as shown in the error.

Also, because you are getting an implicit flash already via request you don't need this line: val flash = play.api.mvc.Flash(Map("error" -> "Please select another id for this client"))

You can just use the request parameter as a Flash one because of the implicit conversion.

To fix the error for missing implicit messages: import play.api.i18n.Messages.Implicits._