Playback form and two submit buttons

I had a form for editing, but I had to add a button to remove the order. Now I have a 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 the controller for an edit-only form:

 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 sent?

+6
source share
3 answers

The value property of the selected button is sent to the server like 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"); } 
+18
source

In addition to Julien Lafont, answer what you can do in the 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") } } 
+10
source

To complement Julien Lafont and OlivierBlanvillain , in the case of a form that is being checked with an error, the Action remains unchanged if this form is simply returned. Thus, filtering this action gives the correct action 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) } ) 
+1
source

All Articles