Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

playframework 2.0 - Play Scala Form Helper with custom HTML

I'm creating a form in Play using the following code:

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

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="">

While I want it to generate:

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

Is there an easy way of doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can achieve this by creating a custom FieldConstructor (see http://www.playframework.com/documentation/2.3.x/ScalaCustomFieldConstructors).

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

Note: If you really need value="", you can add 'value -> "" to the helper's arguments, i.e.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...