Forms 编辑现有对象时在Play Framework 2窗体中渲染字段
这似乎是一个相当基本的问题。我正在使用PlayFramework2.1.5和Scala2.10开发一个CRUD应用程序(将来会有更多)。我也在使用Twitter Bootstrap3.2.0。当我试图更新对象时,我无法使模板呈现窗体的值 我跟着书走。我已经可以创建一个新对象了。现在我希望能够编辑对象Forms 编辑现有对象时在Play Framework 2窗体中渲染字段,forms,scala,playframework,crud,playframework-2.1,Forms,Scala,Playframework,Crud,Playframework 2.1,这似乎是一个相当基本的问题。我正在使用PlayFramework2.1.5和Scala2.10开发一个CRUD应用程序(将来会有更多)。我也在使用Twitter Bootstrap3.2.0。当我试图更新对象时,我无法使模板呈现窗体的值 我跟着书走。我已经可以创建一个新对象了。现在我希望能够编辑对象 产品域对象通过以下方式映射: 同伴Person对象具有相应的CRUD功能Product.findByEan(ean)是一个简单的按字段搜索查询,返回一个选项[Person] Persons控制器具有
产品
域对象通过以下方式映射:
同伴Person
对象具有相应的CRUD功能Product.findByEan(ean)
是一个简单的按字段搜索查询,返回一个选项[Person]
Persons
控制器具有表单[Person]
映射:
private val productForm = Form(mapping(
"ean" -> longNumber.verifying("validation.ean.duplicate", Product.findByEan(_).isEmpty),
"name" -> nonEmptyText,
"description" -> nonEmptyText)(Product.apply)(Product.unapply))
在我的路线中
GET /products controllers.Products.list
POST /products controllers.Products.save
GET /products/new controllers.Products.newProduct
GET /products/$ean<\d{13}> controllers.Products.show(ean: Long)
GET /products/$ean<\d{13}>/edit controllers.Products.edit(ean: Long)
我知道我正在成功地检索对象,因为记录器
消息在播放控制台中清楚地显示了这一点
用于呈现表单的editProducts
模板为:
@(productForm: Form[Product])(implicit flash: Flash, lang: Lang)
@import helper._
@import helper.twitterBootstrap._
@productsLayout(Messages("products.form")) {
<h2>@Messages("products.form")</h2>
@helper.form(action = routes.Products.save()) {
<fieldset>
<legend>
@Messages("products.details", Messages("products.edit"))
</legend>
@helper.inputText(productForm("product.ean"))
@helper.inputText(productForm("product.name"))
@helper.textarea(productForm("product.description"))
</fieldset>
<p><input type="submit" class="btn primary" value='@Messages("products.edit.submit")'</p>
}
}
@(productForm:Form[Product])(隐式flash:flash,lang:lang)
@导入助手_
@导入helper.twitterBootstrap_
@productsLayout(消息(“products.form”)){
@信息(“products.form”)
@helper.form(action=routes.Products.save()){
@消息(“products.details”,消息(“products.edit”))
@helper.inputText(productForm(“product.ean”))
@helper.inputText(productForm(“product.name”))
@text区域(productForm(“product.description”))
广告1
您的问题与引用表单字段名的方式有关。不应将映射对象名作为前缀
@helper.inputText(productForm("ean"))
@helper.inputText(productForm("name"))
@helper.textarea(productForm("description"))
更多信息
值得记住的是,对表单字段的访问是通过提供映射中指定的名称来完成的。指定的字段名称不必与相应的案例类字段名称匹配。如果映射如下所示
private val productForm = Form(mapping(
"id" -> longNumber.verifying("validation.ean.duplicate", Product.findByEan(_).isEmpty),
"full-name" -> nonEmptyText,
"desc" -> nonEmptyText)(Product.apply)(Product.unapply))
您可以通过这种方式访问模板中的字段
@helper.inputText(productForm("id"))
@helper.inputText(productForm("full-name"))
@helper.textarea(productForm("desc"))
广告2
虽然如果您单击Twitter Bootstrap字段构造函数部分中的Twitter Bootstrap链接,字段帮助器工作正常,但您将被重定向到Bootstrap v2.3.2主页,因此我猜此帮助器是为以前的版本设计的
此外,我建议在<强>播放2.3迁移指南中读取引导部分,这解释了为什么要支持Bootstrap的内置支持。您应该考虑创建最新的Bootstrap版本的自定义字段助手。如果您考虑迁移到新版本的Play框架,它将减轻您的痛苦。
谢谢。关于字段,使用错误的字段是一个多么大的错误。我正在创建我自己的字段帮助程序,因为从Play框架中删除Twitter引导帮助程序确实有很好的设计意义。
private val productForm = Form(mapping(
"id" -> longNumber.verifying("validation.ean.duplicate", Product.findByEan(_).isEmpty),
"full-name" -> nonEmptyText,
"desc" -> nonEmptyText)(Product.apply)(Product.unapply))
@helper.inputText(productForm("id"))
@helper.inputText(productForm("full-name"))
@helper.textarea(productForm("desc"))