使用自定义HTML播放Scala Form Helper [英] Play Scala Form Helper with custom HTML
问题描述
我正在使用以下代码在Play中创建表单:
I'm creating a form in Play using the following code:
@inputText(loginForm("password"),
'type -> "password",
'_label -> null)
它生成以下HTML代码:
It generates the following HTML code:
<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="">
有一种简单的方法吗?
推荐答案
您可以通过创建自定义FieldConstructor来实现此目的(请参见
You can achieve this by creating a custom FieldConstructor (see http://www.playframework.com/documentation/2.3.x/ScalaCustomFieldConstructors).
创建一个包含以下内容的新文件views/helper/myPlainFieldConstructor.scala.html
:
Create a new file views/helper/myPlainFieldConstructor.scala.html
that contains the following:
@(elements: helper.FieldElements)
@elements.input
[For reference, you can see the default field constructor here.]
然后,在包含您的表单的视图模板中:
Then, in the view template containing your form:
@import helper._
@implicitField = @{ FieldConstructor(myPlainFieldConstructor.f) }
[...]
@form(action = ...) {
@inputPassword(loginForm("password"))
}
注意:如果您确实需要value=""
,则可以将'value -> ""
添加到帮助程序的参数中,即
Note: If you really need value=""
, you can add 'value -> ""
to the helper's arguments, i.e.
@inputPassword(loginForm("password"), 'value -> "")
或使用通用的input
帮助器进一步自定义HTML,如:
Or further customize the HTML with the generic input
helper, as in:
@input(loginForm("password")) { (id, name, value, args) =>
<input type="password" name="@name" id="@id" value="" @toHtmlArgs(args)>
}
这篇关于使用自定义HTML播放Scala Form Helper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!