【问题标题】:Binding form request with ManyToOne field on play framework 2绑定表单请求与播放框架 2 上的 ManyToOne 字段
【发布时间】:2012-08-03 20:05:19
【问题描述】:

当我向我的模型“项目”添加一个多单字段并尝试绑定相应的表单时,我收到此错误。

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

【问题讨论】:

    标签: java scala playframework playframework-2.0


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多