6

我有编辑订单的表格,但我必须添加删除订单的按钮。现在我有两个提交按钮的表单:

@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>
}

我在控制器中的功能仅用于编辑:

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();
     }
 }

如何处理提交的按钮?

4

3 回答 3

18

所选按钮的value属性作为任何其他字段发送到服务器。因此,在您的控制器内部,您可以在请求正文中访问此值并决定要做什么。

模板:

<button type="submit" name="action" value="edit">Edit order</button>
<button type="submit" name="action" value="remove">Remove order</button>

控制器:

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");
}
于 2012-12-23T00:56:01.407 回答
10

为了补充Julien Lafont的回答,您可以在 Scala 控制器中执行以下操作:

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")
  }
}
于 2013-06-20T08:03:24.330 回答
1

为了补充Julien LafontOlivierBlanvillain的答案,在表单验证错误的情况下。如果该表单刚刚返回,则操作保持不变。因此,过滤掉该动作会在下一次给出正确的动作。

  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)
    }
  )
于 2015-03-30T12:44:53.183 回答