问题
I have a Play 2.2 application which gets, thru an external web service, a JSON with field name, type and constraints info ; the content of this JSON can be different every time (though the overall structure stays same, with just the difference in number of fields etc.).
Now the requirement is to render a HTML form based on the field definition received.
Can someone advise what would be the best way to do this (I don't think the usual play-forms can be very useful here, unless someone can tell how to create a dynamic Mapping
, Form
objects).
One of the ideas I had was to send the JSON to client side and use Angular
to render the form but then I am not sure how will I validate that on server side.
回答1:
Play forms are type-safe, which means that the form contents are statically defined (i.e. a tuple or a case class). You will have to write code to generate the form dynamically and to parse the results.
Generating the HTML for the form is a bit too complex to do in a Scala template. I recommend writing a function in an object to do this, eg:
object MyHelpers {
def makeForm(js: JsObject): Html = {
val xml =
<form method="post">
{ js.values.map { e =>
<input type=text name={e._1} value={e._2}/>
}}
</form>
Html(xml.toString)
}
}
Then import the function in your template: @import MyHelpers.makeForm
, and call it at the point in your page where you want the form.
Parse the form result with an Action
(which in this case must be specified as a POST in your routes file) like so:
def myFormHandler(parse.multipartFormData) { request =>
val data = for ((key, values) <- request.body.dataParts) yield {
// validate and process field
}
OK(view.html.myform())
}
(If your action is a GET, which you probably don't want, use parse.asFormUrlEncoded
)
回答2:
Use freemarker as your templating play-form solution within the Play framework to do it. I did something similar as a proof of concept for a work related project. https://github.com/rabbipal/Play-2.1-Java-MongoDb-Freemarker-Dynamic-Form. Hope it helps.
来源:https://stackoverflow.com/questions/22484772/purely-dynamic-forms-using-play-framework