-
Notifications
You must be signed in to change notification settings - Fork 32
/
errors.go
151 lines (140 loc) · 3.51 KB
/
errors.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Produces a form like:
//
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"github.com/gorilla/schema"
"github.com/joncalhoun/form"
)
var inputTpl = `
<div class="mb-4">
<label class="block text-grey-darker text-sm font-bold mb-2" {{with .ID}}for="{{.}}"{{end}}>
{{.Label}}
</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight {{if errors}}border-red{{end}}" {{with .ID}}id="{{.}}"{{end}} type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}" {{with .Value}}value="{{.}}"{{end}}>
{{range errors}}
<p class="text-red pt-2 text-xs italic">{{.}}</p>
{{end}}
{{with .Footer}}
<p class="text-grey pt-2 text-xs italic">{{.}}</p>
{{end}}
</div>`
func main() {
tpl := template.Must(template.New("").Funcs(form.FuncMap()).Parse(inputTpl))
fb := form.Builder{
InputTemplate: tpl,
}
pageTpl := template.Must(template.New("").Funcs(fb.FuncMap()).Parse(`
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-grey-lighter">
<div class="w-full max-w-md mx-auto my-8">
<form class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4" action="/" method="post">
{{inputs_and_errors_for .Form .Errors}}
<div class="flex items-center justify-between">
<button class="bg-blue hover:bg-blue-dark text-white font-bold py-2 px-4 rounded" type="submit">
Submit
</button>
</div>
</form>
<p class="text-center text-grey text-xs">
© 2018 Acme Corp. All rights reserved.
</p>
</div>
</body>
</html>
`))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
w.Header().Set("Content-Type", "text/html")
data := struct {
Form nestedForm
Errors []error
}{
Form: nestedForm{
Name: "Michael Scott",
Email: "michael@dunder.com",
Address: nil,
},
Errors: []error{
fieldError{
Field: "Email",
Issue: "is already taken",
},
fieldError{
Field: "Address.Street1",
Issue: "is required",
},
fieldError{
Field: "Address.City",
Issue: "is required",
},
fieldError{
Field: "Address.State",
Issue: "must be a US state",
},
fieldError{
Field: "Address.Zip",
Issue: "must be 5 digits",
},
fieldError{
Field: "Address.Zip",
Issue: "is required",
},
},
}
err := pageTpl.Execute(w, data)
if err != nil {
panic(err)
}
return
case http.MethodPost:
default:
http.NotFound(w, r)
return
}
// You can also process these forms using the gorilla/schema package.
r.ParseForm()
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
var form nestedForm
err := dec.Decode(&form, r.PostForm)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(form)
w.Write(b)
})
http.ListenAndServe(":3000", nil)
}
type nestedForm struct {
Name string
Email string
Address *address
}
type address struct {
Street1 string
Street2 string
City string
State string
Zip string `form:"label=Postal Code"`
}
type fieldError struct {
Field string
Issue string
}
func (fe fieldError) Error() string {
return fmt.Sprintf("%v: %v", fe.Field, fe.Issue)
}
// You can implement this however you want - this is just how I'm doing it.
func (fe fieldError) FieldError() (field, err string) {
return fe.Field, fe.Issue
}