1

The problem in a nut shell:

Two forms with more or less identical code, both take a single value "ean". One form works as intended, the other always fails to bind. I Included

println(form.data)

in each controller to see what was going on. When entering the value "h", for example, into each form the working form prints out

Map(ean -> h)

where as the "broken" form prints out

Map()

So why is the second form not binding values?

The story:

I have been doing a project in Scala using the Play framework. Things were going well until I wanted to create a new form. For some reason this form always fails to bind. I couldn't see why this was happening so I decided to make a "copy" of a currently working form, only changing the names of the variables etc. However this "copy" form also has the same problem! I've looked online for a bit of help and the closest problem I can find is the following:

Issue with bindFromRequest in Play! Framework 2.3

But after trying the posted solution it doesn't seem to help at all. Below I've posted the relevant chunks of code, the "ProductPartForm" is the original working form and the "AddDealForm" is the broken copy form. Am I making a silly trivial error somewhere? Any help would be greatly appreciated. Please also note that I'm aware that the "success" message doesn't work (as you can see from the comment), however that shouldn't have any effect on the problem I'm considering.

Thanks!

The code:

Classes:

package models

case class ProductPartForm(ean: Long) {
}

and

package models

case class AddDealForm(ean : Long) {
}

Controller:

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>
}   

Routes:

POST    /Suppliers                  controllers.Suppliers.findByProduct
POST    /Suppliers/b                controllers.Suppliers.addDeal
Community
  • 1
  • 1
C.Anglin
  • 11
  • 2

1 Answers1

0

I had exactly some problem with play 2.4.6 version. In my case problem was that I not specified a request body parser. More about body parsers can be found there: https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers . You should specify body parser in your action (use urlFormEncoded if you use simple form)

def findByProduct = Action(parse.urlFormEncoded) { implicit request =>
}
dpa
  • 427
  • 4
  • 16