-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e979b41
commit 613f369
Showing
6 changed files
with
82 additions
and
17 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
src/docs/cookbook/generating_data_classes_from_json/index.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cookbook.html_forms | ||
|
||
import org.http4k.core.Body | ||
import org.http4k.core.ContentType | ||
import org.http4k.core.Method | ||
import org.http4k.core.Request | ||
import org.http4k.core.with | ||
import org.http4k.lens.FormField | ||
import org.http4k.lens.FormValidator | ||
import org.http4k.lens.Header | ||
import org.http4k.lens.LensFailure | ||
import org.http4k.lens.WebForm | ||
import org.http4k.lens.int | ||
import org.http4k.lens.webForm | ||
|
||
data class Name(val value: String) | ||
|
||
fun main(args: Array<String>) { | ||
|
||
// define fields using the standard lens syntax | ||
val ageField = FormField.int().required("age") | ||
val nameField = FormField.map(::Name, Name::value).optional("name") | ||
|
||
// add fields to a form definition, along with a validator | ||
val strictFormBody = Body.webForm(FormValidator.Strict, nameField, ageField).toLens() | ||
val feedbackFormBody = Body.webForm(FormValidator.Feedback, nameField, ageField).toLens() | ||
|
||
val invalidRequest = Request(Method.GET, "/") | ||
.with(Header.Common.CONTENT_TYPE of ContentType.APPLICATION_FORM_URLENCODED) | ||
|
||
// the "strict" form rejects (throws a LensFailure) because "age" is required | ||
try { | ||
strictFormBody.extract(invalidRequest) | ||
} catch (e: LensFailure) { | ||
println(e.message) | ||
} | ||
|
||
// the "feedback" form doesn't throw, but collects errors to be reported later | ||
val invalidForm = feedbackFormBody.extract(invalidRequest) | ||
println(invalidForm.errors) | ||
|
||
// creating valid form using "with()" and setting it onto the request | ||
val webForm = WebForm().with(ageField of 55, nameField of Name("rita")) | ||
val validRequest = Request(Method.GET, "/").with(strictFormBody of webForm) | ||
|
||
// to extract the contents, we first extract the form and then extract the fields from it using the lenses | ||
val validForm = strictFormBody.extract(validRequest) | ||
val age = ageField.extract(validForm) | ||
println(age) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cookbook.html_forms | ||
|
||
import org.http4k.core.Method | ||
import org.http4k.core.Request | ||
import org.http4k.core.body.form | ||
|
||
fun main(args: Array<String>) { | ||
|
||
val request = Request(Method.GET, "/").form("name", "rita").form("age", "55") | ||
|
||
println(request.form("name")) | ||
println(request.form("age")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
HTML form support is provided on 2 levels: | ||
|
||
1. Through the use of `form()` extension methods on `Request` to get/set String values. | ||
1. Using the Lens system, which adds the facility to define form fields in a typesafe way, and to validate form contents (in either a strict (400) or "feedback" mode). | ||
|
||
### Gradle setup | ||
``` | ||
compile group: "org.http4k", name: "http4k-core", version: "2.28.0" | ||
``` | ||
|
||
### Standard (non-typesafe) API | ||
<script src="https://gist-it.appspot.com/https://github.com/http4k/http4k/blob/master/src/docs/cookbook/html_forms/example_standard.kt"></script> | ||
|
||
### Lens (typesafe, validating) API | ||
<script src="https://gist-it.appspot.com/https://github.com/http4k/http4k/blob/master/src/docs/cookbook/html_forms/example_lens.kt"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters