Scala Template apply style to inputText's label [Play 2 HTML5 helper tag]

戏子无情 提交于 2019-12-06 01:20:05

You could try ditching the built-in field constructors and instead write your own. The following template accepts a custom argument that controls the styling of the label:

app/views/_my_field_constructor.scala.html

@(element: helper.FieldElements)

<div class="clearfix @if(element.hasErrors){error}">
  <label for="@element.id" class="@element.args.get('_label_class)">@element.label</label>
  <div class="input">
    @element.input
  </div>
</div>

Now use your new field constructor instead of whichever built-in one you were using before:

app/views/form.scala.html

....
@* implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) } *@
@implicitField = @{ FieldConstructor(_my_field_constructor.f) }
....

When calling the helper function to create a input text field, you can now pass in a custom _label_class argument that the template will pick up:

app/views/form.scala.html

@inputText(orderItem("item1"), '_label -> "Product", '_label_class -> "red", '_class -> "tinytfss")

I just ran into this problem myself, but I was able to solve it without using a custom FieldConstructor. All I did was change my form helper line to include a form id, and then reference the label for that form in css. Like so:

scala template file:

    <style>
        #new-tenant-form label {
            font-weight: bold;
        }
    </style>

. . .

    @helper.form(routes.Tenants.create(), 'id -> "new-tenant-form") {
           . . . 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!