在堅果殼的問題結合:形式不是在播放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
的代碼看起來正確,哪裏是你的控制器代碼中傳遞的形式到HTML頁面? – josephpconley
對不起!我忘了包含所有的控制器代碼 - 我現在編輯它。 –