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?
yes, multiple submit buttons can include in the html form. One simple example is given below.
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.
Yes, structurally the submit button needs to be inside a form element for the document to be valid X/HTML.
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.
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.
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.
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.
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.
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");
}
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")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With