Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playframework form and two submit buttons

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?

like image 255
Marek Čačko Avatar asked Dec 22 '12 23:12

Marek Čačko


People also ask

Can form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do you handle multiple submit buttons in the same form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Does the submit button have to be in the form?

Yes, structurally the submit button needs to be inside a form element for the document to be valid X/HTML.

Where should submit button be placed in form?

Submit buttons are placed at the bottom of the page to optimize top to bottom flow. There should always be two buttons, a primary action button that will commit changes made by the user and a Cancel button that will abort those changes.

How to render a form in Play Framework?

We need a controller to render the page/view that will show the form to the user. In the Play framework, you should send the form of the case class you created to your view so that it can be rendered: Finally, let’s render our form! As you can see, it’s really easy to render the form. In case you didn’t notice, we added an ‘action’ to the form.

How to add a submit button to a form using JavaScript?

You use this type to add a submit button to forms. When a user clicks it, it automatically submits the form. It takes a value attribute, which defines the text that appears inside the button. An input with a type set to button creates a button, which can be manipulated by JavaScript's onClick event listener type.

How to perform multiple actions with multiple buttons in a form?

Let’s learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method ‘post’ and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

How do you create a button on a form?

When a user clicks it, it automatically submits the form. It takes a value attribute, which defines the text that appears inside the button. An input with a type set to button creates a button, which can be manipulated by JavaScript's onClick event listener type.


2 Answers

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");
}
like image 114
Julien Lafont Avatar answered Dec 06 '22 14:12

Julien Lafont


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")
  }
}
like image 38
OlivierBlanvillain Avatar answered Dec 06 '22 14:12

OlivierBlanvillain