【问题标题】:Implementing DRY Forms实施 DRY 表格
【发布时间】:2012-06-12 00:36:38
【问题描述】:

进入 Play 2.0,总体而言,令人惊叹,很棒的东西。

无论如何,想知道如何在 Play 中实现 DRY 形式?如果您查看Computer Database Sample 中的创建和编辑示例,您会发现表单元素是重复的。只有 4 个字段(在示例表单中),没什么大不了的,但是当您处理大型、复杂的表单和/或处理许多 CRUD 模型时,重复成为一个更大的维护问题。

在绑定端(form.bindFromRequestform.fill(Foo)),创建/编辑操作的实现非常优雅;模板层有对应的解决方案吗?

【问题讨论】:

    标签: forms dry playframework-2.0


    【解决方案1】:

    k,这是我想出的处理注册、续订场景的方法:

    // views.subscription.signup.scala.html
    @(_form: Form[com.company.model.Subscription], target: play.api.mvc.Call)
    @import helper._
    
    @cart("Foobar Registration") {
      <h1>Join Foobar Today!</h1>
      @form(action = target, 'id -> "subscriptionForm", 'class -> "form-horizontal") {
        @fields(_form) // include formfields
      }
    }
    
    // views.subscription.fields.scala.html
    @(_form: Form[com.company.model.Subscription])
    @import helper._
    
    @inputText(field = _form("firstName"), '_label-> "First Name*", 'class-> "required")
    @inputText(field = _form("lastName"), '_label-> "Last Name*", 'class-> "required")
    ....  
    

    然后,对于续订,只需创建与 signup.scala.html 相同的 shell,但带有续订标题和标题文本。

    同样的原则适用于任何共享的创建/编辑表单状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2011-12-25
      相关资源
      最近更新 更多