在Play Framework 2上将表单请求与ManyToOne字段绑定 [英] Binding form request with ManyToOne field on play framework 2
本文介绍了在Play Framework 2上将表单请求与ManyToOne字段绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将多音域添加到我的模型"Item"中时,会出现此错误&我尝试绑定相应的表格.
I get this error when i add a manytoone field to my Model "Item" & i try to bind the corresponding form.
Execution exception
[IllegalStateException: No value] at line 31
=> Item item = itemForm.bindFromRequest().get();
项目" 模型: 封装模型;
the "Item" Model: package models;
@Entity
public class Item extends Model {
@Id
public Long id;
@ManyToOne
@Constraints.Required
@Formats.NonEmpty
public Category category;
@Constraints.Required
public String title;
@Constraints.Required
public String content;
public String picture;
(..)
}
查看表格:
@helper.form(action = routes.Application.newItem(), 'id -> "item_form", 'method -> "POST", 'enctype -> "multipart/form-data"){
<fieldset>
@helper.inputText(
itemForm("title"),
'_label -> "Titre" )
@helper.select(
itemForm("category"),
helper.options(Category.list),
'_label -> "Categorie")
@helper.textarea(
itemForm("content"),
'_label -> "Description")
@helper.inputFile(
field = itemForm("picture"),
'_display -> "Attachment",
'_label -> Messages("Image") )
<input type="submit" value="Ajouter">
</fieldset>
}
控制器:
public static Result newItem(){
Item item = itemForm.bindFromRequest().get(); //SOMETHING GO WRONG HERE
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("picture");
if (picture != null) {
(...)
}
else{
(...)
}
}
推荐答案
考虑到类别模型具有id字段,类别字段的表单视图应该是.
The form view for category field should be, considering Category model has id field.
@helper.select(
itemForm("category.id"),
helper.options(Category.list),
'_label -> "Categorie")
这篇关于在Play Framework 2上将表单请求与ManyToOne字段绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文