I have a simple model object:
case class Region(id: String, revision: Option[String], name: String)
object Region {
// Returns Some(region) if successful, None if revision doesn't match the latest
// in the data store
def insertOrUpdate(region: Region): Promise[Option[Region]]
}
In my controller I want to do something like this but I don’t know how to indicate the lock failure in the response. I would like to add a global form error but can’t see how from the API.
def update(id: String) = Action {
implicit request => regionForm.bindFromRequest.fold(
formWithErrors => BadRequest(views.html.regions.edit(formWithErrors)),
region => Async{
Region.insertOrUpdate(region).map{
_ match {
case None => {
// How do I add a global form error indicating there were server side changes detected
BadRequest(views.html.regions.edit(regionForm.fill(region))
}
case Some(r) => Redirect(views.html.regions.index).flashing(("success", "Update successful")
}
}
)
}
For Play 2.0.4
A global error is in reality an error without key (see
globalErrorsmethod).There is no helper to add an error, but you can do it by yourself, with something like that:
For Play 2.1
You can use the
withGlobalErrormethod: