使用自定义HTML播放Scala Form Helper [英] Play Scala Form Helper with custom HTML

查看:75
本文介绍了使用自定义HTML播放Scala Form Helper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆