-
Notifications
You must be signed in to change notification settings - Fork 2
/
form.go
153 lines (126 loc) · 2.85 KB
/
form.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
152
153
package form
import (
"errors"
"html/template"
"strings"
)
type Form struct {
template template.Template
groupTemplate template.Template
}
func NewForm(template, groupTemplate template.Template) Form {
return Form{
template: template,
groupTemplate: groupTemplate,
}
}
func (f *Form) FuncMap() template.FuncMap {
return template.FuncMap{
"form_render": f.formRender,
}
}
func (f *Form) formRender(v interface{}, errs []FieldError, kv ...any) (template.HTML, error) {
tr, err := NewTransformer(v)
if err != nil {
return "", err
}
fieldErrors := scanError(errs)
var data = make(map[string]any)
if len(kv) > 0 {
if len(kv)%2 != 0 {
return "", errors.New("invalid dict call")
}
for i := 0; i < len(kv); i += 2 {
key, ok := kv[i].(string)
if !ok {
return "", errors.New("dict keys must be strings")
}
data[key] = kv[i+1]
}
}
var html template.HTML
for _, field := range tr.Fields {
if field.Type == FieldTypeGroup {
gtpl, err := f.groupTemplate.Clone()
if err != nil {
return "", err
}
var sb strings.Builder
gtpl = gtpl.Funcs(template.FuncMap{
"fields": func() template.HTML {
var subhtml template.HTML
for _, subField := range field.Fields {
fMap := copyMap(data)
fMap["Field"] = field
fieldhtml, err := f.formFieldHTML(subField, fieldErrors, fMap)
if err != nil {
continue
}
subhtml = subhtml + fieldhtml
}
return subhtml
},
"errors": func() []string {
if errs, ok := fieldErrors[field.Name]; ok {
return errs
}
return nil
},
})
fMap := copyMap(data)
fMap["Field"] = field
err = gtpl.Execute(&sb, fMap)
if err != nil {
return "", err
}
html = html + template.HTML(sb.String())
continue
}
fieldHTML, err := f.formFieldHTML(field, fieldErrors, data)
if err != nil {
return "", err
}
html = html + fieldHTML
}
return html, nil
}
func (f *Form) formFieldHTML(field FormField, errors map[string][]string, data map[string]any) (template.HTML, error) {
tpl, err := f.template.Clone()
if err != nil {
return "", err
}
var sb strings.Builder
tpl = tpl.Funcs(template.FuncMap{
"errors": func() []string {
if errs, ok := errors[field.Name]; ok {
return errs
}
return nil
},
})
fMap := copyMap(data)
fMap["Field"] = field
err = tpl.Execute(&sb, fMap)
if err != nil {
return "", err
}
return template.HTML(sb.String()), nil
}
type FieldError interface {
FieldError() (field, err string)
}
func scanError(errs []FieldError) map[string][]string {
ret := make(map[string][]string)
for _, err := range errs {
field, fieldErr := err.FieldError()
ret[field] = append(ret[field], fieldErr)
}
return ret
}
func copyMap[K, V comparable](m map[K]V) map[K]V {
result := make(map[K]V)
for k, v := range m {
result[k] = v
}
return result
}