Play scala BSONObjectID Form format

334 views
Skip to first unread message

Nicolas Esteban

unread,
Jan 28, 2015, 10:16:13 AM1/28/15
to reacti...@googlegroups.com
Hi,

I'm currently using ReactiveMongo with Play 2.

I'm trying to use a Form by mapping my model class.

But I got an error when I try to bind my id

case class MyModel(_id: Option[BSONObjectID] = None, value:String)

object MyModel {
...
val form = Form(
  mapping("_id"->optional(of[String] verifying pattern("""[a-fA-F0-9]{24}""".r)), "value" -> text)
)(MyModel.apply(MyModel.unapply)

}


Here is the error : not found: value pattern

Is there a specific format to bind BSONObjectID ?

Thanks in advance !

Nicolas Esteban

unread,
Jan 29, 2015, 5:25:05 AM1/29/15
to reacti...@googlegroups.com
I found this :

import play.api.libs.json.Format
import reactivemongo.bson.BSONObjectID

object BSONObjectIDFormat extends Format[BSONObjectID] {
    def writes(objectId: BSONObjectID): JsValue = JsString(objectId.stringify)
    def reads(json: JsValue): JsResult[BSONObjectID] = json match {
      case JsString(x) => {
        val maybeOID: Try[BSONObjectID] = BSONObjectID.parse(x)
        if(maybeOID.isSuccess) JsSuccess(maybeOID.get) else {
          JsError("Expected BSONObjectID as JsString")
        }
      }
      case _ => JsError("Expected BSONObjectID as JsString")
    }
 }

But how can I use it ?

James L. Cain IV

unread,
Jan 29, 2015, 9:26:13 AM1/29/15
to reacti...@googlegroups.com
Probably easier to make your model take a Option[String] and use BSONObjectId.stringify

Jay Cain

Sent from iPhone
--
You received this message because you are subscribed to the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivemong...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James L. Cain IV

unread,
Jan 29, 2015, 9:30:35 AM1/29/15
to reacti...@googlegroups.com
Sorry misunderstood this post.  I don't think you need to verify the BSONObjectId. Can u rely on BSONObjectId.generate?


Jay Cain

Sent from iPhone

On Jan 29, 2015, at 3:25 AM, Nicolas Esteban <n.est...@gmail.com> wrote:

--

Yuriy Grinberg

unread,
Jan 29, 2015, 1:20:03 PM1/29/15
to reacti...@googlegroups.com
I am rather new to play myself
but I think that format that you found is how BSONObjectId reads/writes from/to JSON
and it is already an implicit formatter so if your input was a json string

{"_id":"2354235425265"}

then this code

val id = Json.parse[BSONObjectId](text)

would use that implicit and parse/validate the input text

I have started at some point playing with forms but quickly switched to json/ajax etc.

Nicolas Esteban

unread,
Jan 30, 2015, 2:28:35 AM1/30/15
to reacti...@googlegroups.com
Thanks for the answers guys !
I would like to use model mapping with binding form on request.
Well I see now a other way, probably better, to do what I want.

Thanks !

You received this message because you are subscribed to a topic in the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivemongo/tpKChw0Kzl8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reactivemong...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages