"passwor-6ren">
gpt4 book ai didi

scala - 使用自定义 HTML 玩 Scala Form Helper

转载 作者:行者123 更新时间:2023-12-04 17:59:52 25 4
gpt4 key购买 nike

我正在使用以下代码在 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="">

有没有简单的方法来做到这一点?

最佳答案

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

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

@(elements: helper.FieldElements)

@elements.input

[供引用,可以查看默认字段构造函数here .]

然后,在包含您的表单的 View 模板中:

@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)>
}

关于scala - 使用自定义 HTML 玩 Scala Form Helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24728377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com