Reputation: 1010
I had form for editing order but I had to add button for removing order. Now I have form with two submit buttons:
@helper.form(routes.Order.editOrder,'class -> "form-horizontal") {
@helper.inputText(
PlayMagicForJava.javaFieldtoScalaField(editOrderForm("date")),
'_label -> "Date:",
'_help -> ""
)
@helper.inputText(
PlayMagicForJava.javaFieldtoScalaField(editOrderForm("place_from")),
'_label -> "From:",
'_help -> ""
)
<button type="submit" name="edit" id="edit" class="btn btn-primary">Edit Order</button>
<button type="submit" name="remove" id="remove" value="remove" class="btn">Remove order</button>
}
My function in controler for form just for editing:
public static Result editOrder(){
Order user = User.findByEmail(session("email"));
Form<Order> editOrderFormFilled = editOrderForm.bindFromRequest();
Order order = Order.findByID(editOrderFormFilled.get().id);
if(editOrderFormFilled.hasErrors()) {
return badRequest();
}
else if(user.id != order.created_by){
return badRequest();
}else{
return OK();
}
}
How can I handle which button was submited?
Upvotes: 7
Views: 11553
Reputation: 7768
To complemente Julien Lafont's answer, here is what you could do in a Scala Controller:
def handle = Action { implicit request =>
request.body.asFormUrlEncoded.get("action").headOption match {
case Some("edit") => Ok("Clicked edit")
case Some("remove") => Ok("Clicked remove")
case _ => BadRequest("This action is not allowed")
}
}
Upvotes: 10
Reputation: 7877
The value
property of the selected button is sent to the server as any other fields. So, inside your controller, you can access this value in the request body and decide what to do.
Template:
<button type="submit" name="action" value="edit">Edit order</button>
<button type="submit" name="action" value="remove">Remove order</button>
Controller:
public static Result myAction() {
String[] postAction = request().body().asFormUrlEncoded().get("action");
if (postAction == null || postAction.length == 0) {
return badRequest("You must provide a valid action");
} else {
String action = postAction[0];
if ("edit".equals(action)) {
return edit(request());
} else if ("remove".equals(action)) {
return remove(request());
} else {
return badRequest("This action is not allowed");
}
}
}
private static Result remove(Request request) {
// TODO
return ok("implement your business here");
}
private static Result edit(Request request) {
// TODO
return ok("implement your business here");
}
Upvotes: 19
Reputation: 898
To complement Julien Lafont's and OlivierBlanvillain's answer, in the case of an form that validates with an error. The action stays the same if that form just gets returned. So filtering out that action gives a correct action the next time.
val postAction: String = request.body.asFormUrlEncoded.get("action").head
// bind the form request.
val bindFromRequest: Form[CaseClass] = CaseOriginalForm.bindFromRequest
bindFromRequest.fold(
formWithErrors => {
// filter out the action button data (otherwise the first action comes back)
val filterNot: Map[String, String] = formWithErrors.data.filterNot(f => f._1 == "action")
val formWithErrors2= CaseOriginalForm.bind(filterNot)
BadRequest(views.html.FormPage(formWithErrors2))
},
contact => {
Ok( "Succes page " + postAction)
}
)
Upvotes: 1