I am still pretty new to the play framework and I am currently trying to get the values on post when the there is an error. I need these values to display the information based on the IDs that being passed as hidden values.
Here is my updateOperatorsPost controller method:
val updateOpForm = Form(
tuple(
"op_id" -> longNumber(min = 0),
"ad_id" -> longNumber(min = 0),
"name" -> nonEmptyText(maxLength = 50, minLength = 2),
"status" -> nonEmptyText(maxLength = 255, minLength = 2),
"city" -> nonEmptyText(maxLength = 50, minLength = 2),
"stateName" -> nonEmptyText(maxLength = 20, minLength = 2),
"street1" -> nonEmptyText(maxLength = 50, minLength = 2),
"street2" -> text(maxLength = 50),
"zip" -> longNumber(min = 1)
)
)
def updateOperatorPost = Action{implicit request =>
updateOpForm.bindFromRequest.fold(
errors=>BadRequest(html.updateOperator(errors, Operator(0, 0, "A", "B"), Address(0, "A", "B", "C", "D", "E"))),
{case (op_id, ad_id, name, status, city, stateName, street1, street2, zip) =>
models.Operators.update(op_id, ad_id, name, status, city, stateName, street1, street2, zip.toString)
Ok(html.index(models.Operators.retrieveAllOperators()))
}
)
}
Here is my form:
@(updateOpForm: Form[(Long, Long, String, String, String, String, String, String, Long)], op:Operator, ad:Address)
@import helper._
@main("Update Operator"){
<a style="font-size: 25px; text-decoration: none" href="/"><- Home</a> <br />
<form action="/updateOperatorPost" method="post">
<input type="hidden" name="op_id" value="@op.id"/>
<input type="hidden" name="ad_id" value="@ad.id" />
@helper.input(updateOpForm("name")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@op.name"@toHtmlArgs(args)>
}
@helper.input(updateOpForm("status")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@op.status"@toHtmlArgs(args)>
}
<h2>Address</h2>
@helper.input(updateOpForm("city")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@ad.city"@toHtmlArgs(args)>
}
@helper.input(updateOpForm("stateName")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@ad.stateName"@toHtmlArgs(args)>
}
@helper.input(updateOpForm("street1")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@ad.street1"@toHtmlArgs(args)>
}
@helper.input(updateOpForm("street2")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@ad.street2"@toHtmlArgs(args)>
}
@helper.input(updateOpForm("zip")) { (id, name, value, args) =>
<input type="text" name="@name" id="@id" value="@ad.zip"@toHtmlArgs(args)>
}
<input type="submit" id="submit">
</form>
}
As you can see I am passing some made up objects just to see the form when there is an error, I would like to pull in the data based on the ids that are hidden in the form. Is this possible? I hope I made this clear enough.
You should be able to get the value directory from the request body
def updateOperatorPost = Action{implicit request =>
updateOpForm.bindFromRequest.fold(
errors=> {
val opId = (request.body \ "op_id") asOpt[String] getOrElse "op_id was not included in the request"
// Do something with opId
BadRequest(html.updateOperator(errors, Operator(0, 0, "A", "B"), Address(0, "A", "B", "C", "D", "E")))
},
{case (op_id, ad_id, name, status, city, stateName, street1, street2, zip) =>
models.Operators.update(op_id, ad_id, name, status, city, stateName, street1, street2, zip.toString)
Ok(html.index(models.Operators.retrieveAllOperators()))
}
)
}
Here is the bit of code that helped me
val op_id = (request.body.asFormUrlEncoded.get("op_id")(0)).toLong
Thanks for the help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.