【问题标题】:How to allow default value selection in a @select helper?如何允许在 @select 帮助器中选择默认值?
【发布时间】:2013-05-17 10:09:47
【问题描述】:

在我的表单中,我定义了一个下拉菜单:

@select(
myForm("category_id"),
options(Category.options()),
'_label -> "Category",
'_default -> "-- Choose a Category --",
'_showConstraints -> false
)

在我的控制器代码中:

Form<Category> catForm = form(Category.class).bindFromRequest();
if(catForm.hasErrors()) {
return badRequest(categoryEdit.render(catForm));
}

表单提交不允许我选择默认值,如果我没有选择,则 catForm.hasErrors() 为 true。两个问题:

  1. 如何允许在下拉列表中选择默认值?

  2. 我希望默认值为-1,在哪里设置呢? (也许这就是问题所在,-- Choose a Category -- 选项没有关联的值?)

【问题讨论】:

    标签: playframework playframework-2.0 scala-template


    【解决方案1】:

    aviks 的建议正在奏效。可能您没有正确导入模板。 我是这样做的。首先,我按照 avik 的建议在 views/helper/ 中创建了一个 customSelectField.scala.html

    @(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*)(implicit handler: FieldConstructor, lang: play.api.i18n.Lang)
    
    @getAsTuple(x : Any) = @{
        x match {
            case (value: String, text: String) => (value, text)
            case _ => ("-1", "Select")
        }
    }
    
    @input(field, args:_*) { (id, name, value, htmlArgs) =>
        <select id="@id" name="@name" @toHtmlArgs(htmlArgs)>
    
    
    @args.toMap.get('_default).map { dv =>
        <option class="blank" value="@getAsTuple(dv)._1">@getAsTuple(dv)._2</option>
    }
    
    @options.map { v =>
        <option value="@v._1" @(if(value == Some(v._1)) "selected" else "")>@v._2</option>
    }
    </select>
    }
    

    然后在我的模板中,例如 index.scala.html 我想要选择的地方:

    @import helper._ 
    
    @helper.customSelectField(
     field = proposeNewTimeForm("selectTime"),
     options = times.get,
     '_label -> "Category",
     '_default -> ("-1" -> "-- Choose a category --"),
     '_showConstraints -> false
    )
    

    记住你应该这样做:

    @implicitField = @{
            FieldConstructor(helper.customSelectField.f)
        }
    

    因为这会导致你的错误。

    如果您想以某种方式格式化围绕选择的 html,您可以像我一样使用 customField.scala.html 在views/helper/:

    @(elements: helper.FieldElements)
    
    @elements.input
    <span class="errors">@elements.errors.mkString(", ")</span>
    <span class="help">@elements.infos.mkString(", ")</span>
    

    然后在index.scala.html的顶部:

    @import helper._
    @implicitField = @{
        FieldConstructor(helper.customField.f)
    }
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢 Jakob,如果需要任何进一步的帮助,我会尽力回复。
    【解决方案2】:

    您可以编写一个替代Play's HTML select helper 的替代方案,它接受一个元组作为默认选项。通过这种方式,您可以指定基础值和应显示的文本。

    这是第一次尝试,这里的一些 Scala 诚然有点业余:

    app/views/_my_select.scala.html

    @(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*)(implicit handler: helper.FieldConstructor, lang: play.api.i18n.Lang)
    
    @import helper.input
    
    @getAsTuple(x : Any) = @{
      x match {
        case (value: String, text: String) => (value, text)
        case _ => ("-1", "Select")
      }
    }
    
    @input(field, args:_*) { (id, name, value, htmlArgs) =>
      <select id="@id" name="@name" @toHtmlArgs(htmlArgs)>
    
        @args.toMap.get('_default).map { dv =>
          <option class="blank" value="@getAsTuple(dv)._1">@getAsTuple(dv)._2</option>
        }
    
        @options.map { v =>
          <option value="@v._1" @(if(value == Some(v._1)) "selected" else "")>@v._2</option>
        }
      </select>
    }
    

    用法

    @_my_select(
      myForm("category_id"),
      options(Category.options()),
      '_label -> "Category", 
      '_default -> ("-1" -> "-- Choose a category --"),
      '_showConstraints -> false
    )
    

    【讨论】:

    • 我收到此错误--> 类型不匹配;找到: (play.api.data.Field, Seq[(String, String)], Array[(Symbol, Any)]) => play.api.templates.Html required: views.html.helper.FieldElements => play .api.templates.Html 注意:隐式方法implicitFieldConstructor 在这里不适用,因为它位于应用程序点之后,并且缺少显式结果类型
    【解决方案3】:

    我遇到了类似的问题,并找到了一个更简单的解决方案。
    而不是玩!帮助者在您的表单中尝试此操作:

    <select name="category_id">
        <option value="-1">-- Choose a Category --</option>
        @(for((key, value) <- Category.options()){
            <option value="@value"> @key </option>
        }
    </select>
    

    【讨论】:

    • 谢谢阿图拉斯,会试一试
    猜你喜欢
    • 2016-12-31
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    相关资源
    最近更新 更多