2016-07-25 19 views
0

我是新玩家!我遇到了一些總是有錯誤的表單。即使所有字段都填滿了,我也無法弄清楚問題所在。玩!表格總是有錯誤

路線

GET  /products/     controllers.Products.list() 
GET  /products/new    controllers.Products.newProduct() 
POST /products/     controllers.Products.save() 

產品的Controller.java

import play.data.Form; 

private final static Form<Product> productForm = form(Product.class); 

public static Result list() { 
    List<Product> productList = Product.findAll(); 
    return ok(list.render(productList)); 
} 

public static Result newProduct() { 
    return ok(details.render(productForm)); 
} 

public static Result save() { 
    Form<Product> boundForm = productForm.bindFromRequest(); 
    if(boundForm.hasErrors()) { 
     flash("error", 
       "Please correct the form below."); 
     return badRequest(details.render(boundForm)); 
    } 

    // For mystery reasons, in this line, product is always null 
    // Product product = boundForm.get(); 

    Product product = new Product(); 
    product.ean = boundForm.data().get("ean"); 
    product.name = boundForm.data().get("name"); 
    product.description = boundForm.data().get("description"); 
    product.save(); 

    flash("success", 
      String.format("Successfully added product %s", product)); 

    return redirect(routes.Products.list()); 
} 

產品的Model.java

import static play.data.validation.Constraints.Required; 

public class Product { 

    @Required 
    public String ean; 
    @Required 
    public String name; 
    public String description; 
    ... 
} 

產品的form.scala.html

@(productForm: Form[Product]) 


@main("Product form") { 
    <h1>Product form</h1> 
    @helper.form(action = routes.Products.save()) { 
     <fieldset> 
      <legend>Product (@productForm("name").valueOr("New"))</legend> 

      @helper.inputText(productForm("ean"), '_label -> "EAN") 
      @helper.inputText(productForm("name"),'_label -> "Name") 

      @helper.textarea(productForm("description"), '_label -> "Description") 

     </fieldset> 
     <input type="submit" class="btn btn-primary" value="Save"> 
     <a class="btn" href="@routes.Products.list()">Cancel</a> 
    } 
} 

這裏是調試器的截圖,有數據和錯誤太:(

Using debugger

我在做什麼錯?

~~~~更新~~~~~

我增加了列表路由和控制器動作

這裏是回購:

https://github.com/LTroya/up-and-running-play

+0

你錯過了一些文件'潰敗es.Products.list'沒有在'routes'和controller中定義,main.scala.html缺失。十分鐘內從頭開始複製是很困難的。 –

+0

@AndrzejJozwik完成。我添加了列表路由,處理列表的控制器操作以及github存儲庫 – LTroya

回答

1

的解決方案 - 你需要豆在java實現(安裝人員丟失):

public class Product { 

    @Required 
    public String ean; 
    @Required 
    public String name; 
    public String description; 
     public String getEan() { 
     return ean; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setEan(String ean) { 
     this.ean = ean; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 
+0

哦。非常感謝你,它的工作原理。 – LTroya