2012-08-03 74 views
2

我得到這個錯誤,當我添加一個manytoone領域我的模型「項目」&我試圖綁定相應的表單。綁定形式請求與ManyToOne字段在播放框架2

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

回答

7

的類別字段表單視圖應該考慮範疇模型具有ID字段。

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