Is it possible to override form helpers?

2019-02-15 02:19发布

Using the doc, I can set my own helper for the layout surrending my field, but I'd like to personalize also some fields given by play.

The main reason is for Twitter Bootstrap 2, where I need to change (in checkbox.scala.html)

@input(field, args:_*) { (id, name, value, htmlArgs) =>
    <input type="checkbox" id="@id" name="@name" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value))>
    <span>@args.toMap.get('_text)</span>
}

to :

<label class="checkbox">
    <input type="checkbox" name="@name" id="@id" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value)) />
    @args.toMap.get('_text)
</label>

How can I do that ? Thanks for your help!

2条回答
Summer. ? 凉城
2楼-- · 2019-02-15 02:57

It will be simpler to just write your own tag with the code you want and use it instead of the provided helper. It will simplify potential issues related to overwritting platform tags.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-15 03:13

I finally did it like this :

I created a package views.helpers.form, that contains :

bootstrap.scala.html :

@(elements: helper.FieldElements)

<div class="control-group@if(elements.hasErrors) { error}">
    <label class="control-label" for="@elements.id">@elements.label(elements.lang)</label>
    <div class="controls">
        @elements.input
        @elements.infos(elements.lang).map { info =>
            <span class="help-inline">@info</span>
        }
        @elements.errors(elements.lang).map { error =>
            <span class="help-block">@error</span>
        }
    </div>

checkbox.scala.html :

@**
 * Generate an HTML input checkbox.
 *
 * Example:
 * {{{
 * @checkbox(field = myForm("done"))
 * }}}
 *
 * @param field The form field.
 * @param args Set of extra HTML attributes ('''id''' and '''label''' are 2 special arguments).
 * @param handler The field constructor.
 *@
@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: helper.FieldConstructor, lang: play.api.i18n.Lang)

@boxValue = @{ args.toMap.get('value).getOrElse("true") }

@helper.input(field, args:_*) { (id, name, value, htmlArgs) =>
    <label class="checkbox">
        <input type="checkbox" id="@id" name="@name" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value))>
        @args.toMap.get('_text)
    </label>


div>
</div>

And in my template, all I have to do is :

@import helper.{FieldConstructor, inputText, inputPassword} @** Import the original helpers *@
@import helpers.form.checkbox @** Import my helpers *@
@implicitField = @{ FieldConstructor(helpers.form.bootstrap.f) }

And voilà! It works!

查看更多
登录 后发表回答