簡體   English   中英

在Play Framework 2上將表單請求與ManyToOne字段綁定

[英]Binding form request with ManyToOne field on play framework 2

當我在我的模型“ Item”中添加一個多音域字段時,會出現此錯誤,並且我嘗試綁定相應的表單。

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

考慮到類別模型具有id字段,類別字段的表單視圖應為。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM