Skip to content

Commit 3859024

Browse files
committed
Handle form arrays.
1 parent 15ba4ef commit 3859024

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func main() {
2323

2424
authorized := r.Group("/", authMiddleware.MiddlewareFunc())
2525
authorized.GET("/", adminView.Index)
26+
authorized.POST("/", adminView.SendLoad)
2627

2728
r.Run(":8000")
2829
}

templates/index.tmpl

+24
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
<h1>Hello, Loadcentral Admin!</h1>
22
<a href="/logout">Logout</a>
3+
4+
<form method="POST" id="loadcentral_form">
5+
<label for="phone_number">Phone number: </label>
6+
<input type="text" name="phone_number[]">
7+
<label for="pcode">Pcode: </label>
8+
<select name="pcode[]">
9+
<option value="GMX100">Globe 100</option>
10+
<option value="SM100">Smart 100</option>
11+
</select>
12+
<br>
13+
14+
<label for="phone_number">Phone number: </label>
15+
<input type="text" name="phone_number[]">
16+
<label for="pcode">Pcode: </label>
17+
<select name="pcode[]">
18+
<option value="GMX100">Globe 100</option>
19+
<option value="SM100">Smart 100</option>
20+
</select>
21+
<br>
22+
23+
<input type="submit">
24+
</form>
25+
26+
<p>{{ .error }}</p>

views/admin.go

+14
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,17 @@ type Admin struct{}
1010
func (admin Admin) Index(c *gin.Context) {
1111
c.HTML(http.StatusOK, "index.tmpl", nil)
1212
}
13+
14+
func (admin Admin) SendLoad(c *gin.Context) {
15+
type sendLoadForm struct {
16+
PhoneNumber []string `form:"phone_number[]" binding:"required"`
17+
Pcode []string `form:"pcode[]" binding:"required"`
18+
}
19+
20+
var form sendLoadForm
21+
if err := c.ShouldBind(&form); err != nil {
22+
c.HTML(http.StatusOK, "index.tmpl", gin.H{"error": err.Error()})
23+
return
24+
}
25+
c.HTML(http.StatusOK, "index.tmpl", nil)
26+
}

0 commit comments

Comments
 (0)