播放框架绑定表单字段到复选框

时间:2013-03-21 18:34:56

标签: data-binding twitter-bootstrap playframework playframework-2.0

如何绑定myForm中名为" isMarried"的字段?到视图中的复选框控件?我希望这种双向绑定。我正在使用https://github.com/nostalgiaz/bootstrap-switch

中的自举开关
@(myForm: Form[_])
@helper.form(action = controllers.index.submit, 'id -> "myForm") {

<fieldset>
<legend>General information</legend>
<div class="twipsies well">
   <div class="control-group">
        <label class="control-label" for="isMarried">Married</label>
        <div class="controls">
            <div class="switch switch-small" tabindex="0" data-on-label="Yes" data-off-label="No" data-off="warning">
                <input id="isMarried" name="isMarried" type="checkbox"/>
            </div>
        </div>
    </div>

</div>

</fieldset>
}

2 个答案:

答案 0 :(得分:4)

我没有使用复选框(在Pay中),但我会使用复选框助手:

示例:

@checkbox(field = myForm("done"))

参见来源:https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/views/helper/checkbox.scala.html

如果您需要自定义HTML以满足您的需求,请编写您自己的帮助/标记:

祝你好运!

答案 1 :(得分:0)

模型是:

/**

 * This class represent a school model 

 * @author Giovanni Alberto García

 * @version 1.0

 */

@Entity

public class School extends Model {


  @Id

  @Column(unique=true, nullable=false)

  public Long id;


  @ManyToMany(cascade = CascadeType.ALL)

  public List<User> members;


  public boolean rElecCFE;

...

}

正如您所见,rElecCFE是布尔值,因此视图为:

@(schoolForm: Form[School])(schoolId: Long)

@import helper._ 

@main("Completar registro") {

<div id="edition">

  @form(action=routes.Nama.doEdit()) {
  <fieldset id="registration-electicity">
    <input type="hidden" name="id" value="@schoolId"/>
    <legend>Electricidad</legend>
    @checkbox(schoolForm("rElecCFE"),
    '_showConstraints->false,
    '_disabled->""
    )
...
  }
</div>
}