如何在 playframework 中预先 select radiobutton- / checkbox-group?

How to pre-select radiobutton- / checkbox-group in the playframework?

我正在尝试使用 Play!Framework 2.3.8 构建一个简单的表单。因此,我创建了一个单选按钮组,效果很好,但是您如何预先 select 某个单选按钮?

我在文档中没有找到任何相关内容:

Documentation radiobuttons

复选框存在同样的问题:

Documentation checkboxes

@helper.form(action = routes.Application.submit(), 'id -> "userForm"){
        <fieldset>
            @helper.inputRadioGroup(
            userForm("Geschlecht"),
            options = options("Mann"->"Mann","Frau"->"Frau"),
            '_label -> "Gender",
            '_error -> userForm("Geschlecht").error.map(_.withMessage("select gender"))
            )
        </fieldset>

有什么想法吗?

当您在呈现时将表单对象传递到视图中时,该表单对象必须填充一个实例,该实例具有 selected 字段值 Geschlecht

控制器代码

UserForm user = new UserForm();
//set value of Geschlecht
user.Geschlecht = Mann;

Form<UserForm> form = Form.form(UserForm.class);
form.fill(user)

Html view = views.index.render(userForm);

index.scala.html

@helper.form(action = routes.Application.submit(), 'id -> "userForm"){
        <fieldset>
            @helper.inputRadioGroup(
            userForm("Geschlecht"),
            options = options("Mann"->"Mann","Frau"->"Frau"),
            '_label -> "Gender",
            '_error -> userForm("Geschlecht").error.map(_.withMessage("select gender"))
            )
        </fieldset>

在选项映射中,它将 select 条目与字段 Geschlecht 的值具有相同的键。