forked from bluele/gforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_input.go
57 lines (52 loc) · 1.17 KB
/
radio_input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"github.com/bluele/gforms"
"net/http"
"path"
"runtime"
"text/template"
)
type Lang struct {
Name string `gforms:"name"`
}
func main() {
tpl := template.Must(template.ParseFiles(path.Join(getTemplatePath(), "post_form.html")))
langForm := gforms.DefineModelForm(Lang{}, gforms.NewFields(
gforms.NewTextField(
"name",
gforms.Validators{
gforms.Required(),
},
gforms.RadioSelectWidget(
map[string]string{},
func() gforms.RadioOptions {
return gforms.StringRadioOptions([][]string{
{"Golang", "golang", "false", "false"},
{"Python", "python", "false", "false"},
{"C", "c", "false", "true"},
})
},
),
),
))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
form := langForm(r)
if r.Method != "POST" {
tpl.Execute(w, form)
return
}
if !form.IsValid() {
tpl.Execute(w, form)
return
}
lang := form.GetModel().(Lang)
fmt.Fprintf(w, "ok: %v", lang)
})
http.ListenAndServe(":9000", nil)
}
func getTemplatePath() string {
_, filename, _, _ := runtime.Caller(1)
return path.Join(path.Dir(filename), "templates")
}