twitter bootstrap表格水平排列字段 - 播放框架

时间:2013-03-23 01:58:02

标签: twitter-bootstrap playframework playframework-2.0

我正在尝试使用twitter bootstrap框架在play框架中构建一个表单。

我想在多行中安排控件。我还希望获得bootstrap提供的验证消息。有没有办法做到这一点?

<form class="form">

    @****
    * This works
    *****@

    <div class="controls controls-row">
        <input type="text" id="inputWarning" placeholder="placeholder">
        <input type="text" id="inputWarning" placeholder="placeholder">
    </div>

    @****
    * Why doesn't this work?
    *****@
    <div class="controls controls-row">
        <div class="control-group warning">
            <label class="control-label" for="inputWarning">Input with warning</label>
            <div class="controls">
                <input type="text" id="inputWarning">
                <span class="help-inline">Something may have gone wrong</span>
            </div>
        </div>
        <div class="control-group warning">
            <label class="control-label" for="inputWarning">Input with warning</label>
            <div class="controls">
                <input type="text" id="inputWarning">
                <span class="help-inline">Something may have gone wrong</span>
            </div>
        </div>
    </div>

</form>

1 个答案:

答案 0 :(得分:2)

您可以使用Twitter bootstrap documents中描述的form-horizontal类来构建水平表单。

播放提供Form template helpers,使渲染表单变得更加容易。不幸的是,Play附带的Twitter Bootstrap模板帮助程序不能使用水平表单,而且会得到deprecated in the next play version

我认为最好的解决方案是编写自己的Bootstrap模板助手,如f.ex. app/views/twitterBootstrapInput.scala.html

@(elements: helper.FieldElements)  

<div class="control-group @if(elements.hasErrors) {error}">
  @if(elements.label.toString.nonEmpty) { <label class="control-label" for="@elements.id">@elements.label</label> }
  <div class="controls">
  @elements.input

    <p class="help-inline">@elements.infos.mkString(", ")</p>  
}  
    @if(elements.hasErrors) { <p class="help-block">@elements.errors.mkString(", ")</p> }  
  </div>
</div>

然后你可以建立一个像这样的水平形式:

@(args...)

@import helper._
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.f) } 

@helper.form(routes.myRoute(args), 'class -> "form-horizontal") {
   @select(
     editOptions("highlightStyle"),
     Seq("abc" -> "def", "ghi" -> "jkl"),
     '_label -> "Source style:"
   )
}