Re: [play-framework][deprecated] Updating only changed values into MongoDB from a Form Submission

18 views
Skip to first unread message

Will Sargent

unread,
May 22, 2018, 7:37:16 PM5/22/18
to play-fr...@googlegroups.com
Form specific case classes and object persistence models are very different, and you should map between them explicitly.  See https://www.playframework.com/documentation/2.6.x/ScalaForms#Defining-a-form for details.

You should check if your browser is actually sending a PATCH request to the server.  Many browsers only use GET and POST.

You may also want to check out the following for REST API example: https://developer.lightbend.com/guides/play-rest-api/

On Tue, May 8, 2018 at 8:36 AM, Get Yeti <getye...@gmail.com> wrote:

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 (ScalaForm 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.

--
Please join our new forum at https://discuss.playframework.com!
The play-framework Google Group will soon be put into read-only mode.
For details, see https://blog.playframework.com/introducing-discuss-playframework-com/.
---
You received this message because you are subscribed to the Google Groups "Play Framework [deprecated]" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/882e29e5-3fb8-4755-b138-92e4286dc843%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Will Sargent

unread,
May 23, 2018, 7:56:43 PM5/23/18
to Play Framework [deprecated]
Reply all
Reply to author
Forward
0 new messages