1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 20:07:04 +00:00

add bind checkboxes example

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-01 11:39:57 +03:00
parent dbeb7bfb73
commit c7157f1c92
4 changed files with 56 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./templates", ".html"))
app.Get("/", showForm)
app.Post("/", handleForm)
app.Listen(":8080")
}
func showForm(ctx iris.Context) {
ctx.View("form.html")
}
type formExample struct {
Colors []string `form:"colors[]"` // or just colors, it'll work as expected.
}
func handleForm(ctx iris.Context) {
var form formExample
err := ctx.ReadForm(&form)
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.JSON(iris.Map{"Colors": form.Colors})
}

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Select a color</title>
</head>
<body>
<form action="/" method="POST">
<p>Select one or more colors</p>
<label for="red">Red</label>
<!-- name can be "colors" too -->
<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>
</body>
</html>