是否可以覆盖表单助手? [英] Is it possible to override form helpers?

查看:80
本文介绍了是否可以覆盖表单助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用该文档,我可以为自己的字段设置布局自己的助手,但是我也想对游戏提供的某些字段进行个性化设置.

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.

主要原因是我需要更改Twitter Bootstrap 2(在checkbox.scala.html中)

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

至:

<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!

推荐答案

我终于做到了:

我创建了一个包含views:helpers.form的包:

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

bootstrap.scala.html:

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:

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

瞧瞧!可行!

这篇关于是否可以覆盖表单助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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