Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form-binder doc #116

Open
wants to merge 2 commits into
base: 2.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _includes/_upper_body_common.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<li><a href="{{site.baseurl}}guides/formats/upload.html">File Upload</a></li>
<li><a href="{{site.baseurl}}guides/formats/json.html">JSON</a></li>
<li><a href="{{site.baseurl}}guides/formats/commands.html">Commands</a></li>
<li><a href="{{site.baseurl}}guides/formats/formbinder.html">Form Binder</a></li>
</ul>
</li>
<!-- END FORMS -->
Expand Down
103 changes: 103 additions & 0 deletions guides/formats/formbinder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
layout: guide
title: Form Binder | Formats | Scalatra guides
---

<div class="page-header">
<h1>Form Binder</h1>
</div>


Form-binder is a micro data binding and validating framework, easy to use and hack.

## Usage at a glance

```scala
import com.github.tminglei.bind.simple._

val binder = FormBinder(messages, errsTree())

val mappings = tmapping(
"id" -> long(),
"body" -> (expandJson() >-: tmapping(
"email" -> (required() >+: text(email())),
"price" -> (omitLeft("$") >-: float()),
"count" -> number().verifying(min(3), max(10))
).label("xx").verifying { case (label, (email, price, count), messages) =>
if (price * count > 1000) {
Seq(s"$label: $price * $count = ${price * count}, too much!")
} else Nil
})
)

binder.bind(mappings, params).fold(
errors => errors,
{ case (id, (email, price, count)) =>
// do something here
}
)

```

1) define your binder
2) define your mappings
3) prepare your data
4) bind and consume


## Installation & Integration

#### 1. Add the dependency to your sbt project file:

```scala
libraryDependencies += "com.github.tminglei" %% "form-binder" % "0.10.0"
```

#### 2. Define your helper trait:

```scala
object FormBinderSupport {
val BindMessagesKey = "__bind_messages"
}

trait FormBinderSupport extends I18nSupport { self: ScalatraBase =>
import FormBinderSupport._

before() {
request(BindMessagesKey) = Messages(locale, bundlePath = "bind-messages")
}

def binder(implicit request: HttpServletRequest) = FormBinder(bindMessages.get, errsTree())

///
private def bindMessages(implicit request: HttpServletRequest): Messages = if (request == null) {
throw new ScalatraException("There needs to be a request in scope to call bindMessages")
} else {
request.get(BindMessagesKey).map(_.asInstanceOf[Messages]).orNull
}
}
```

#### 3. Then, mix and use it in your app codes:

```scala
import org.scalatra.ScalatraServlet
import com.github.tminglei.bind.simple._

class SampleServlet extends ScalatraServlet with FormBinderSupport {

get("/:id") {
val mappings = tmapping(
"id" -> long()
)
binder.bind(mappings, data(multiParams)).fold(
errors => holt(400, errors),
{ case (id) =>
Ok(toJson(repos.features.get(id)))
}
)
}
}
```

That's it. _See the [project site](https://github.com/tminglei/form-binder) for more details._
2 changes: 1 addition & 1 deletion guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ title: Scalatra guides | Scalatra
[wro4j](resources/wro4j.html)

<h4>Formats</h4>
[File Upload](formats/upload.html), [JSON](formats/json.html), [Commands](formats/commands.html), XML, forms
[File Upload](formats/upload.html), [JSON](formats/json.html), [Commands](formats/commands.html), [Form Binder](formats/formbinder.html), XML, forms

<h4>Persistence</h4>
[Introduction](persistence/introduction.html), [MongoDB](persistence/mongodb.html), [Riak](persistence/riak.html),[Slick](persistence/slick.html), [Squeryl](persistence/squeryl.html)
Expand Down