Skip to content

Commit

Permalink
docs(readme): add binding html checkbox example, close gin-gonic#129 (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
javierprovecho authored and Tony Yip committed Feb 20, 2018
1 parent 14f7e90 commit 6f6223e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,52 @@ func startPage(c *gin.Context) {
}
```

### Bind HTML checkboxes

See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)

main.go

```go
...

type myForm struct {
Colors []string `form:"colors[]"`
}

...

func formHandler(c *gin.Context) {
var fakeForm myForm
c.Bind(&fakeForm)
c.JSON(200, gin.H{"color": fakeForm.Colors})
}

...

```

form.html

```html
<form action="/" method="POST">
<p>Check some colors</p>
<label for="red">Red</label>
<input type="checkbox" name="colors[]" value="red" id="red" />
<label for="green">Green</label>
<input type="checkbox" name="colors[]" value="green" id="green" />
<label for="blue">Blue</label>
<input type="checkbox" name="colors[]" value="blue" id="blue" />
<input type="submit" />
</form>
```

result:

```
{"color":["red","green","blue"]}
```

### Multipart/Urlencoded binding

```go
Expand Down

0 comments on commit 6f6223e

Please sign in to comment.