2

モデル「アイテム」に manytoone フィールドを追加し、対応するフォームをバインドしようとすると、このエラーが発生します。

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

1 に答える 1

7

カテゴリ モデルに id フィールドがあることを考慮すると、カテゴリ フィールドのフォーム ビューは次のようになります。

@helper.select(
        itemForm("category.id"), 
        helper.options(Category.list),
        '_label -> "Categorie")
于 2012-08-04T06:16:32.113 に答える