2016-01-07 46 views
1

在坚果壳的问题结合:形式不是在播放2.4.6框架

两种形式具有更多或更少相同代码,无论采取单一值“EAN”。一种形式按预期工作,另一种形式总是不能约束。 I包括

println(form.data) 

在每个控制器,看看发生了什么。当输入值“H”,例如,到每个形成工作形式打印出

Map(ean -> h) 

其中作为“断”的形式打印出

Map() 

那么,为什么第二形式不结合值?

故事:

我一直在使用Play框架做Scala中的一个项目。事情进展顺利,直到我想创建一个新的表单。出于某种原因,此表单总是无法绑定。我看不出为什么会发生这种情况,所以我决定制作一个当前工作表单的“副本”,只更改变量名称等。但是,这种“复制”表单也有同样的问题!我在网上找了一些帮助,我能找到最接近的问题如下:

Issue with bindFromRequest in Play! Framework 2.3

但是,试图在发布的解决方案后,它似乎并没有帮助的。下面我已经发布了相关的代码块,“ProductPartForm”是原始工作表单,“AddDealForm”是破碎的副本表单。我在某处做了一个愚蠢的小错误吗?任何帮助将不胜感激。另请注意,我知道“成功”信息不起作用(正如您可以从评论中看到的那样),但是这对于我正在考虑的问题应该没有任何影响。

谢谢!

的代码:

类:

package models 

case class ProductPartForm(ean: Long) { 
} 

package models 

case class AddDealForm(ean : Long) { 
} 

控制器:

package controllers 

class Suppliers extends Controller { 
    private val productForm: Form[ProductPartForm] = Form(mapping("ean" -> longNumber)(ProductPartForm.apply)(ProductPartForm.unapply)) 

    private val dealForm: Form[AddDealForm] = Form(mapping("ean" -> longNumber)(AddDealForm.apply)(AddDealForm.unapply)) 

    def supplierList = Action { 
    implicit request => 
     val suppliers = Supplier.findAll 
     Ok(views.html.supplierList(suppliers, productForm, dealForm)) 
    } 

    def findByProduct = Action { implicit request => 
    val newProductForm = productForm.bindFromRequest() 
    newProductForm.fold(
     hasErrors = { form => 
      println(form.data) 
      val message = "Incorrent EAN number! Please try again." 
      Redirect(routes.Suppliers.supplierList()).flashing("error" -> message) 
     }, 
     success = { newProduct => 
      val productSuppliers = Supplier.findByProduct(newProductForm.get.ean) 
      val message2 = "It worked!" //can't display message? 
      Ok(views.html.supplierList(productSuppliers, productForm ,dealForm)).flashing("success" -> message2) 
     } 
    ) 
    } 

    def addDeal = Action { implicit request => 
    val newDealForm = dealForm.bindFromRequest() 
    dealForm.fold(
     hasErrors = { form => 
      println(form.data) 
      val message = "Incorrent EAN number! Please try again." 
      Redirect(routes.Suppliers.supplierList()).flashing("error" -> message) 
     }, 
     success = { newDeal => 
      val message2 = "a" 
      Redirect(routes.Products.list).flashing("success" -> message2) 
     } 
    ) 
    } 

HTML:

@helper.form(action = routes.Suppliers.findByProduct()) { 
    <fieldset style="margin-left:200px"> 
    <legend> 
     @helper.inputText(productForm("ean")) 
    </legend> 
    </fieldset> 
    <div style="padding-bottom:60px"> 
     <input type="submit" class="btn primary" value="Submit" style="margin-left:400px"> 
    </div> 
} 

@helper.form(action = routes.Suppliers.addDeal()) { 
    <fieldset style="margin-left:200px"> 
    <legend> 
     @helper.inputText(dealForm("ean")) 
    </legend> 
    </fieldset> 
    <div style="padding-bottom:60px"> 
     <input type="submit" class="btn primary" value="Submit" style="margin-left:400px"> 
    </div> 
} 

路线:

POST /Suppliers     controllers.Suppliers.findByProduct 
POST /Suppliers/b    controllers.Suppliers.addDeal 
+0

的代码看起来正确,哪里是你的控制器代码中传递的形式到HTML页面? – josephpconley

+0

对不起!我忘了包含所有的控制器代码 - 我现在编辑它。 –

回答

0

我正好有一些问题,发挥2.4.6版本。在我的情况下,问题是我没有指定请求正文解析器。有关身体解析器的更多信息,请访问: https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers。 你应该在你的动作中指定身体解析器(使用urlFormEncoded如果你用简单的形式)

def findByProduct = Action(parse.urlFormEncoded) { implicit request => 
}