这就是我得到的:
控制器
val form = Form(
mapping(
"id" -> optional(number),
"name" -> optional(nonEmptyText),
"supID" -> number,
"price" -> longNumber)(Coffee.apply)(Coffee.unapply) )
val extraForm = Form(
mapping(
"coffeeId" -> number,
"amountOfDrinkers" -> number
)(Extra.apply)(Extra.unapply) )
def showForm = Action { implicit request =>
Ok(views.html.show_form(form, extraForm)
}
//And then form validation & insertion
def save = Action { implicit request =>
form.bindFromRequest.fold(
// The second parameter for createForm would supposly pass the second form
// with or without errors
formWithErrors => BadRequest(html.coffees.createForm(formWithErrors, /*secondForm?*/ )),
entity => {
database withTransaction {
Coffees.insert(entity)
// So far we have worked & inserted the val form
// But how can I also work & inser the extraForm ?
}
})
}
1.我怎样才能在同一个表单(例如外键)中有一个额外的输入,这些输入将被验证并插入到不同的表中?
2. id 是可选的,我在case类中定义为Option。是否有人可以修改 Post 并插入 id(没有 id 输入),在模型中它被设置为 PrimaryKey 和 AutoIncrease,我想防止任何外部更改。