使用自定义HTML播放Scala表单助手

时间:2014-07-14 01:14:06

标签: scala playframework-2.0

我正在使用以下代码在Play中创建表单:

@inputText(loginForm("password"),
                        'type -> "password",
                        '_label -> null)

它会生成以下HTML代码:

<dl class=" " id="password_field">
<dt><label for="password"></label></dt>
<dd>
<input type="password" id="password" name="password" value="">

虽然我希望它能够生成:

<input type="password" id="password" name="password" value="">

有没有一种简单的方法可以做到这一点?

1 个答案:

答案 0 :(得分:7)

您可以通过创建自定义FieldConstructor来实现此目的(请参阅http://www.playframework.com/documentation/2.3.x/ScalaCustomFieldConstructors)。

创建一个包含以下内容的新文件views/helper/myPlainFieldConstructor.scala.html

@(elements: helper.FieldElements)

@elements.input

[作为参考,您可以看到默认字段构造函数here。]

然后,在包含表单的视图模板中:

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

[...]

@form(action = ...) {
  @inputPassword(loginForm("password"))
}

注意:如果您确实需要value="",可以将'value -> ""添加到帮助者的参数中,即

@inputPassword(loginForm("password"), 'value -> "")

或者使用通用input帮助程序进一步自定义HTML,如:

@input(loginForm("password")) { (id, name, value, args) =>
    <input type="password" name="@name" id="@id" value="" @toHtmlArgs(args)>
}
相关问题