尝试更新 NUTRITION 表的值时,我的表单出现问题。编辑表单适用于仅涉及项目但不涉及嵌套对象 (NUTRITION) 的任何内容。尽管对于 NUTRITION,填充工作正常,并且表单接收数据。但是,当我重新填充表单时,之后的填充值显示为零。
查看/编辑表格
@(id: Long, itemForm: Form[Item])
@import helper._
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.render) }
@main {
<div class="panel panel-primary" style="font-family: Crumble;font-weight:bold">
<div class="panel-heading">
<h1 class="panel-title">Edit Item</h1>
</div>
@form(routes.Admin.update(id)) {
<fieldset>
<legend>Item Information</legend>
@inputText(itemForm("name"), '_label -> "Item name")
@inputText(itemForm("description"), '_label -> "Item description")
@inputText(itemForm("image_url"), '_label -> "Image URL")
@inputText(itemForm("nutrition.serving_size"), '_label -> "Serving Size (G)")
@inputText(itemForm("nutrition.calories"), '_label -> "Calories")
@inputText(itemForm("nutrition.calories_fat"), '_label -> "Calories From Fat")
@inputText(itemForm("nutrition.total_fat"), '_label -> "Total Fat (G)")
@inputText(itemForm("nutrition.sat_fat"), '_label -> "Saturated Fat")
@inputText(itemForm("nutrition.trans_fat"), '_label -> "Trans Fatty Acids (G)")
@inputText(itemForm("nutrition.cholesterol"), '_label -> "Cholesterol (MG)")
@inputText(itemForm("nutrition.sodium"), '_label -> "Sodium (MG)")
@inputText(itemForm("nutrition.carbs"), '_label -> "Carbohydrates (G)")
@inputText(itemForm("nutrition.fiber"), '_label -> "Dietary Fiber (G)")
@inputText(itemForm("nutrition.sugar"), '_label -> "Sugars (G)")
@inputText(itemForm("nutrition.protein"), '_label -> "Protein (G)")
@inputText(itemForm("nutrition.vitamin_c"), '_label -> "Vitamin C (%DV)")
@inputText(itemForm("nutrition.calcium"), '_label -> "Calcium (%DV)")
@inputText(itemForm("nutrition.iron"), '_label -> "Iron (%DV)")
@select(
itemForm("category.id"),
options(Category.options),
'_label -> "Category", '_default -> "-- Choose a category --",
'_showConstraints -> false
)
</fieldset>
<div class="footer">
<div class="form-actions">
<input type="submit" value="Save this item" class="btn btn-primary"> or
<a href="@routes.Admin.adminItems()" class="btn">Cancel</a>
</div>
}
</div>
@form(routes.Admin.delete(id), 'class -> "topRight") {
<input style="margin-right:25px;margin-top:100px" type="submit" value="Delete this item" class="btn btn-danger">
}
</div>
}
控制器
public static Result edit(Long id) {
Form<Item> itemForm = form(Item.class).fill(Item.find.byId(id));
return ok(editItemForm.render(id, itemForm));
}
public static Result update(Long id) {
Form<Item> itemForm = form(Item.class).bindFromRequest();
if(itemForm.hasErrors()) {
return badRequest(editItemForm.render(id, itemForm));
}
itemForm.get().update(id);
flash("success", "item " + itemForm.get().name + " has been updated");
return GO_ADMIN;
}
public static Result create() {
Form<Item> itemForm = form(Item.class);
return ok(
createItemForm.render(itemForm)
);
}
public static Result save() {
Form<Item> itemForm = form(Item.class).bindFromRequest();
if(itemForm.hasErrors()) {
return badRequest(createItemForm.render(itemForm));
}
itemForm.get().save();
flash("success", "item " + itemForm.get().name + " has been created");
return GO_ADMIN;
}
打印出
Form Information View for Edit:
Some(models.Item@a)
{}
Form Information Updated:
Some(models.Item@28ddd490)
{nutrition.trans_fat=0.0, nutrition.carbs=1, category.id=3, nutrition.sat_fat=1.
5, nutrition.sugar=0, nutrition.serving_size=55, nutrition.sodium=220, nutrition
.total_fat=10.0, nutrition.vitamin_c=0, image_url=images/items/subs/bacon_egg_ch
eese.jpg, nutrition.cholesterol=10, description=Start the day with sizzling Beec
hwoodΓÇá Smoked Back Bacon, free range egg omelette and melted cheese.ΓÇÖ., nutr
ition.calcium=0, name=Bacon, Egg and Cheese, nutrition.calories=100, nutrition.p
rotein=0, nutrition.calories_fat=90, nutrition.iron=0, nutrition.fiber=0}
Form Information View for Edit:
Some(models.Item@a)
{}