Reputation: 416
I have a request form on two pages of my site: About us and Contacts. Actions for both of them described in my Home controller. After submitting the form I call sendRequest method, which checks form errors and sends request. This method looks like:
def sendRequest = Action {implicit request =>
requestForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(views.html.Home.contacts(formWithErrors))
},
response => {
Redirect(routes.HomeController.contacts().flashing("success" -> "OK"))
}
)
}
In this method I define view and route to navigate. And for my About us page I should do the same work, but call about us view and route.
How could I create generic method and call it with concrete view and route as parameters?
Upvotes: 0
Views: 301
Reputation: 6124
// if requestForm is a Form[A]
protected def genericMethod(
view: Form[A] => play.twirl.api.Html, route: play.api.mvc.Call) =
Action {implicit request =>
requestForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(view(formWithErrors))
},
response => {
Redirect(route.flashing("success" -> "OK"))
}
)
}
def sendRequest = genericMethod(
views.html.Home.contacts,
routes.HomeController.contacts())
Upvotes: 0