Updating only changed values into MongoDB from a Form Submission


(Samuel) #1

I’ve been setting up a REST API in the Play Framework ( 2.6 ) in Scala and been trying to implement the use of a ( Scala ) Form but I’m having some difficulties when attempting to find the best practice for updating only values that have changed upon form submission. I have used this article as a reference (as well as others) and it uses the HTTP Request PATCH shown as something similar to this with the Routes file:

   PATCH  /update/:collName    controllers.IndexC.update(collName: String, oId: Option[BSONObjectID])

However if I route to this in my ( Play ) template for the form action like this:

   @form(action = routes.IndexC.update(collName, oId), 'method -> "patch", ...)

Then it brings up an error that it cannot locate the file (trying to use a GET request). I’ve read somewhere that this can happen if the browser doesn’t support PATCH as a HTTP Request . Or perhaps I am completely misunderstanding this - we’ll call that part 1 of my gap in knowledge .

Also ( part 2 of my gap in knowledge ) I am struggling to understand how only the values from the form submission can be retrieved (rather than all input box values). So suppose that I had a very simple Scala model & form looking like this:

   case class Countries(countryName: String, countryCode: String, currencyCode: String)

   object Countries {

     val form = Form(
       mapping(
         "countryName" -> nonEmptyText,
         "countryCode" -> nonEmptyText,
         "currencyCode" -> nonEmptyText
       )(Countries.apply)(Countries.unapply)
     )

   }

Is there a way bindFromRequest can help to retrieve just those input values that have changed (from the Mongo database collection and those that were fed initially into the form - using Countries.form.fill ). Or is this perhaps better done elsewhere with a more sensible approach? Or maybe part 1 's solution makes this irrelevant. Many thanks.