Przeglądaj źródła

docs(readme): add binding html checkbox example, close #129 (#994)

Javier Provecho Fernandez 8 lat temu
rodzic
commit
df3b79e805
1 zmienionych plików z 46 dodań i 0 usunięć
  1. 46 0
      README.md

+ 46 - 0
README.md

@@ -481,6 +481,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