在play框架2上使用ManyToOne字段绑定表单请求

时间:2012-08-03 20:05:19

标签: java scala playframework playframework-2.0

当我向模型“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{
              (...)
            }
}

1 个答案:

答案 0 :(得分:7)

类别字段的表单视图应该是,考虑到类别模型有id字段。

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