与多对一场装订形式要求玩框架2(Binding form request with ManyToOn

2019-09-20 18:13发布

我得到这个错误,当我一个多对一字段添加到我的模型“项目”&我尝试绑定相应的形式。

Execution exception
[IllegalStateException: No value] at line 31

=> Item item = itemForm.bindFromRequest().get();

“项目” 型号 :包款;

@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{
              (...)
            }
}

Answer 1:

对于类别字段的形式观点应该是,考虑到分类模型具有ID字段。

@helper.select(
        itemForm("category.id"), 
        helper.options(Category.list),
        '_label -> "Categorie")


文章来源: Binding form request with ManyToOne field on play framework 2