Purely Dynamic forms using play framework

Dev Maha

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.

Richard Close

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scala Play Framework - Dynamic Forms

From Dev

Play Framework Forms (Java)

From Dev

Play framework dynamic template include

From Dev

Play framework dynamic template include

From Dev

Using Realm in a dynamic framework?

From Dev

Using Realm in a dynamic framework?

From Dev

Dynamic Chart using Xamarin Forms

From Dev

Dynamic Chart using Xamarin Forms

From Dev

Display Dynamic Image in Play! Framework Template

From Dev

Long dynamic routes in play framework 2

From Dev

Play Framework - Register a custom DataBinder for dynamic fields

From Dev

Dynamic value of textarea with Play framework with Silhouette

From Dev

Play Framework, Dynamic statement inside for loop

From Dev

Using SORM with Play Framework 2.3.8

From Dev

Using Play!Framework 2.2 subprojects

From Dev

Using Play!Framework 2.2 subprojects

From Dev

Web development using Play! framework

From Dev

Play Framework using View Tags

From Dev

Using dynamic library in static framework or using static library in dynamic framework

From Dev

Using dynamic models in Django framework

From Dev

Using dynamic models in Django framework

From Dev

How to Validate Negative BigDecimals in scala play Framework FORMS?

From Dev

How to Validate Negative BigDecimals in scala play Framework FORMS?

From Dev

Posting to web forms using dynamic inputs

From Dev

Posting via Ajax with dynamic forms using foreach

From Dev

Play framework: How to set error message for specific field in dynamic form

From Dev

Finding single object in database using Play Framework

From Dev

Setting a Foreign Key using ebean in Play Framework

From Dev

Using a BodyParser with an authenticated request in Play 2.2.1 Framework

Related Related

HotTag

Archive