填写预填充数据的表格

时间:2014-07-09 08:40:18

标签: java forms playframework playframework-2.0

我有一个表格供新员工填写。但是现在,我希望有一个编辑表单,它返回用户填写的表单。

这是我的控制者:

    public static Result updateForm(String id) {
    Form<staffInfo> existingStaffForm = Form.form(staffInfo.class);
    staffInfo existingStaff = staffInfo.find.byId(id);
    return ok(
            views.html.update.render(existingStaffForm.fill(existingStaff))
            );
}

这是我的scala.html:

@(existingStaffForm: Form[staffInfo])

@import helper._
@implicitField = @{ FieldConstructor(myFieldConstructorTemplate.f) }

@main("Employment application") {

    @form(routes.Application.newForm(), 'class -> "form-horizontal") {

        <div class = "row">
        <div class="col-md-6">@inputText(existingStaffForm("fullName"), 'size->50,'_label -> "Full name:", '_help -> "*", '_showConstraints -> false)</div>
        <div class="col-md-6">@select(
                                existingStaffForm("sex"),
                                options(Seq("Male", "Female")),
                                '_label -> "Sex:")</div>
        </div>
    }
}

当我在浏览器上运行时,它会返回错误[RuntimeException: Cannot fill a form with a null value]。 在线控制器 - &gt;返回确定。

更新

我发现了主要问题!我有一个更新&#39;显示页面上的按钮供用户单击(如果他们想要更新他们的信息),按钮将被定向到编辑表单页面(代码在上面)。以下是&#39;更新&#39;的代码。按钮:

<a href="/staff/:id/update"><input type="button" class="btn btn-default" value="Update"></a>

我的路线:

GET     /staff/:id/update       controllers.Application.updateForm(id:String)

我认为,问题出在href标签上。有人可以帮助如何以正确的方式放置链接吗?

2 个答案:

答案 0 :(得分:0)

显然existingStaffnull,以某种方式调试它。即:

public static Result updateForm(String id) {
    Form<staffInfo> existingStaffForm = Form.form(staffInfo.class);
    staffInfo existingStaff = staffInfo.find.byId(id);

    if (existingStaff==null) {
        return badRequest("Oooops existingStaff not found in DB... ");
    }

    return ok(
            views.html.update.render(existingStaffForm.fill(existingStaff))
            );
}

顺便说一句:按照惯例,模型的类名应以大写字母开头:StaffInfo

答案 1 :(得分:0)

您应该使用反向路由生成一个href。

@routes.WhateverYourControllerIsNamed.updateForm("id")

请参阅http://www.playframework.com/documentation/2.1.1/ScalaRouting

相关问题