Reputation: 1775
I have two tables A and B where...
Fields for A: (id, companyID)
Fields for B: (id, product, qty)
I have to pass a companyID into the form and create a new row in the table. However, I need to also pass a string of items and their quantity through this form. But I'm not sure how to do that since the Form in the html will type check against it and it will also fail because the Model is only expecting a companyID and not a string of items along with it. How do I pass that piece of information so that I have access to it in my controller? I need it so I can query other data.
Edit. (This is what html looks like)
@(orderForm: Form[Order])
@form(routes.CustomersController.submitOrder) {
@inputText(orderForm("depotID"))
<input type="submit" value="Create" class="btn btn-large btn-mobile-block btn-blue">
}
However, I need to have an additional input field where I can put in a value, that the controller will then have access to.
Upvotes: 1
Views: 1017
Reputation: 2796
You are not limited to passing a single variable (e.g. form) to views. You could, for example pass products : List[Product]
(containing your items and quantities) to your form as a second variable. Typically I would have a main form and supporting variables - the supporting info is not submitted, it is just there to support the form in some way. E.g. in view:
@(companyForm:Form[Company], products:List[Product])
And in the controller, statements like
Ok(views.html.company.add(companyForm, Product.list())
You don't give a lot of info so it's hard to know if this is answering your question exactly. It would be helpful to see a bit more of your code. Also, take a look at the Play samples and the Scala Forms page, which I have found very helpful.
Edit: if you wish to pass extra items (such as a Seq[Product]
) from the view to the controller, then you could use a custom case class that includes the list, and let your controller or model split it out into the actual model case class. E.g.
case class ComputerProduct(id: Int, companyId: Int, products: Seq[Product])
val computerProductForm: Form[ComputerProduct] = Form(mapping(
"id" -> number,
"companyId" -> number,
"products" -> seq(mapping(
"id" -> number,
"product" -> text,
"quantity" -> number)(Product.apply)(Product.unapply)))
(ComputerProduct.apply)(ComputerProduct.unapply))
Upvotes: 1