Skip to content

Commit 7bd297f

Browse files
committed
Merge pull request #8 from decebals/forms
Document form processing
2 parents e6c3757 + 07d33f7 commit 7bd297f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

_posts/2015-06-01-forms.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
layout: page
3+
title: "Form Processing"
4+
category: doc
5+
date: 2015-06-01 10:11:42
6+
order: 55
7+
---
8+
9+
### Cross-Site Request Forgery (CSRF) Protection
10+
11+
See [Security](security.html).
12+
13+
### Form Tricks
14+
15+
Pippo supports several convenient tricks for making HTML form submission more useful in a RESTful world.
16+
17+
#### Indexed Arrays
18+
19+
You may POST Arrays, Lists, or Sets to your `RouteHandler` implementations by using `[n]` suffixes for your parameter names in your forms.
20+
21+
```html
22+
<form>
23+
<input name="rainbow[0]" value="Red">
24+
<input name="rainbow[1]" value="Orange">
25+
<input name="rainbow[2]" value="Yellow">
26+
<input name="rainbow[3]" value="Green">
27+
<input name="rainbow[4]" value="Blue">
28+
<input name="rainbow[5]" value="Indigo">
29+
<input name="rainbow[6]" value="Violet">
30+
</form>
31+
```
32+
33+
In this example, Pippo will automatically collect the values into a parameter named *rainbow*. The *rainbow* parameter may be accessed and transformed to a collection of your choice.
34+
35+
```java
36+
POST("/rainbow", (routeContext) -> {
37+
List<String> rainbowList = routeContext.getParameter("rainbow").toList(String.class);
38+
Set<String> rainbowSet = routeContext.getParameter("rainbow").toSet(String.class);
39+
TreeSet<String> rainbowTreeSet = routeContext.getParameter("rainbow").toCollection(TreeSet.class, String.class);
40+
}
41+
```
42+
43+
Or if you are using Pippo controllers...
44+
45+
```java
46+
public void postRainbow(@Param("rainbow") TreeSet<String> rainbow) {
47+
// do something with the rainbow
48+
}
49+
```
50+
51+
52+
#### Browser-based PUT, PATCH, DELETE, etc
53+
54+
Pippo supports overriding the `POST` method used to submit a form through the use of a hidden form field named `_method`.
55+
56+
```html
57+
<form>
58+
<input name="rainbow[3]" value="Gray">
59+
<input type="hidden" name="_method" value="PATCH">
60+
</form>
61+
```
62+
63+
Pippo will intercept the `_method` parameter specification and route the request to the `PATCH` handler that matches the URL pattern.
64+
65+
```java
66+
PATCH("/rainbow", (routeContext) -> {
67+
List<String> patched = routeContext.getParameter("rainbow").toList(String.class);
68+
List<String> rainbow = dao.getRainbow();
69+
for (int i = 0; i < patched.size(); i++) {
70+
if (patched.get(i) != null) {
71+
rainbow.set(i, patched.get(i));
72+
}
73+
}
74+
dao.updateRainbow(rainbow);
75+
}
76+
```
77+
78+
This allows you to develop a more RESTful design when handling form submissions.
79+
80+
#### Browser-based submission of JSON or another format
81+
82+
Pippo supports POSTing non-form content by specifying `_content_type` and `_content` hidden form fields. When used in combination with the `_method` hidden form field you can exercise a RESTful api.
83+
84+
```html
85+
<form>
86+
<input type="hidden" name="_content" value="['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']">
87+
<input type="hidden" name="_content_type" value="application/json">
88+
<input type="hidden" name="_method" value="PUT">
89+
</form>
90+
```
91+
92+
```java
93+
PUT("/rainbow", (routeContext) -> {
94+
List<String> rainbow = routeContext.createEntityFromBody(List.class);
95+
});
96+
97+
```

0 commit comments

Comments
 (0)