Play Framework:验证成功时访问表单

问题描述 投票:0回答:3

我在Play 2.6中提交了一个表单,其中大多数验证都无法预先执行。 Web应用程序将提交的表单数据发送到另一个项目的后端,这将为大多数用户错误抛出GrammarException。如何将错误消息和原始表单值传播回视图

这与How to access my forms properties when validation fails in the fold call?类似,但我需要成功的形式值。

form.bindFromRequest().fold(
  formWithErrors => {
    BadRequest(myView(newForm = formWithErrors)(request))
  },
  data => try {
    val results = MyBackend.build(data) // time-consuming
    Ok(views.html.myView(results)
  } catch { // catches most user errors
    case e: GrammarException =>
      val submittedForm = ....? //
      val formWithErrors = submittedForm.withGlobalError(e.getMessage)
      BadRequest(myView(newForm = formWithErrors)(request))
  }
)
scala playframework
3个回答
1
投票

您已经拥有包含请求中所有数据的表单,因此您可以使用它。

val formWithBoundData = form.bindFromRequest()
formWithBoundData.fold(
  formWithErrors => {
    BadRequest(myView(newForm = formWithErrors)(request))
  },
  data => try {
    val results = MyBackend.build(data) // time-consuming
    Ok(views.html.myView(results)
  } catch { // catches most user errors
    case e: GrammarException =>
      val formWithErrors = formWithBoundData.withGlobalError(e.getMessage)
      BadRequest(myView(newForm = formWithErrors)(request))
  }
)

1
投票

我建议在处理对另一个组件(模型,api等)的调用时不要在代码中使用try/catch,这就是为什么:

尽可能编写并发代码:在Scala / Play中,我们可以使用Future这个奇妙的世界来编写并发代码。不需要“捕捉”任何东西,我们宁愿使用recover,当方法有Future的东西(例如,Future[Unit])。所以尽可能多地编写并发代码。 Try / catch是类似Java的代码,可能会对问题所在的位置产生误解;这让我想到了下一点。

关注点分离会发生什么?:如果我在我的控制器中尝试/捕获,当我在我的模型中调用/使用方法时,事实是controller错误处理model的错误?为什么?如果模型有错误,控制器应该知道它的确切类型吗?或者控制器应该只知道存在错误(哪种类型不是他/她的业务),并返回对视图的InternalServerError响应;还是BadRequesst

以下代码调用模型/后端,映射返回结果,如果出现错误,它将Future恢复为BadRequest

form.bindFromRequest().fold(
  formWithErrors => {
    BadRequest(myView(formWithErrors)(request))
  },
  givenData =>  
    MyBackend.build(data).map{
      _ => Ok(views.html.myView(results) //When case is successful
    }.recover{ 
    //Any other exception that may occur while calling the other components other than the controller.
   case _       => {
      val formWithErrors = ??? 
      BadRequest(FormWithErrors)
   } 
   //Could build on that if you want to match an exact exception like GrammerException
  }
} 

上述方法的好处:

并发和关注点分离:已经解释了原因。

缩放到多个调用:稍后如果你有新的约束,你仍然可以使用上面的方法使用flatMap进行外部调用,使用map进行内部调用并映射到他们的成功和失败。

更多例外:如果您需要知道确切的异常并向视图提供最佳错误/警告消息,您可以使用更多recover类型在exception块内扩展。


0
投票

从我所看到的,Form的case类有一个错误字段:https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/data/Form.scala#L37

您可以复制收到的表格,添加错误,然后将其退回,不是吗?

© www.soinside.com 2019 - 2024. All rights reserved.