Scala play 框架中的表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16857687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:20:46  来源:igfitidea点击:

Forms in Scala play framework

scalaplayframework-2.0

提问by shashank

Hello i am a beginner to scala play framework. I am unable to create form with two or more inputs. i googled it and found none in scala programming language. Kindly suggest me an idea regarding how to create multiple inputs in a form using scala. i did this

您好,我是 Scala Play 框架的初学者。我无法使用两个或多个输入创建表单。我用谷歌搜索它并没有在 Scala 编程语言中找到。请建议我一个关于如何使用 Scala 在表单中创建多个输入的想法。我做了这个

val form = Form (tuple
    (
"firstname"-> text,
"lastname" -> text
)
)  and to get the values val(fname,lname) = form.bindFromRequest.get

am i following correct way. Kindly suggest me any idea or resource to learn scala play framework . Thanks in advance

我遵循正确的方法吗?请建议我学习 Scala Play 框架的任何想法或资源。提前致谢

回答by Matt Roberts

Here is a complete (but simple) form example for Play 2.1.1. Including the view, controller and routes file. I suspect you were missing importsand / or an implicit request. Both of which would be understandable!

这是 Play 2.1.1 的完整(但简单)表单示例。包括视图、控制器和路由文件。我怀疑您缺少导入和/或隐式请求。这两种情况都可以理解!

The controller (Application.scala):

控制器(Application.scala):

package controllers

import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._

object Application extends Controller {
  val form = Form(
    tuple(
      "firstname" -> text,
      "lastname" -> text
    )
  )

  def index = Action {
    Ok(views.html.index())
  }

  def submit = Action { implicit request =>
    val (fname, lname) = form.bindFromRequest.get
    Ok("Hi %s %s".format(fname, lname))
  }
}

The view (index.scala.html):

视图(index.scala.html):

<!DOCTYPE html>
<html>
  <head>
    <title>Form example</title>
  </head>
  <body>
    <form method="post" autocomplete="on">
      First name:<input type="text" name="firstname"><br>
      Last name: <input type="text" name="lastname"><br>
      <input type="submit">
    </form>
  </body>
</html>

And the routes:

和路线:

GET     /                           controllers.Application.index
POST    /                           controllers.Application.submit

NB: The name attributes in your HTML view must match the string literals in your controller form.

注意:HTML 视图中的名称属性必须与控制器表单中的字符串文字相匹配。

Hope that helps.

希望有帮助。

回答by pamu

Play documentation is the best way to learn about forms https://www.playframework.com/documentation/2.1.1/ScalaFormsif you want more then, please have a look at play-example-form project.

Play 文档是了解表单的最佳方式 https://www.playframework.com/documentation/2.1.1/ScalaForms如果您想要更多,请查看 play-example-form 项目。

http://typesafe.com/activator/template/play-example-formThis activator example project on forms explains everything about forms.

http://typesafe.com/activator/template/play-example-form这个关于表单的激活器示例项目解释了关于表单的一切。

1) It explains forms and data binding, validation in the play controller.

1)它解释了表单和数据绑定,播放控制器中的验证。

2) It explains about Optional parametersin the forms.

2)Optional parameters在表格中说明。

3) It explains about complex forms with nested objects for example

3) 例如,它解释了具有嵌套对象的复杂表单

     case class Student(name: String, age: Int, gender: Optional[Char] = None, 
                                                address: Address, other: OtherStuff)

The above case class depends on Address, OtherStuff and notice that the gender is optional. The example project explains how to deal with such complex object dependencies.

上面的案例类取决于 Address、OtherStuff 并注意性别是可选的。示例项目解释了如何处理这种复杂的对象依赖关系。

Please download activator from here http://typesafe.com/get-started. then, start the activator in browser UI mode using the command activator uiand type play-example-form in the search to download it. After downloading go to the project root folder and type activator eclipsein case you use eclipse or activator gen-ideain case you Intellij Idea.

请从这里下载激活器http://typesafe.com/get-started。然后,使用命令在浏览器 UI 模式下启动激活器activator ui并在搜索中键入 play-example-form 以下载它。下载后转到项目根文件夹并键入activator eclipse以防您使用 eclipse 或activator gen-ideaIntellij Idea。