-
Notifications
You must be signed in to change notification settings - Fork 0
/
formfn.go
46 lines (35 loc) · 906 Bytes
/
formfn.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
package tailush
import (
"html/template"
"github.com/gobuffalo/helpers/hctx"
"github.com/gobuffalo/tags/v3"
)
// FormFn returns a helper function to build forms. The resulting function can be used
// as replacement for the original form helper.
func FormFn(formOptions ...FormHelperOption) any {
return func(opts tags.Options, help hctx.HelperContext) (template.HTML, error) {
if opts == nil {
opts = tags.Options{}
}
hn := defaultFormVar
if n, ok := opts["var"]; ok {
hn = n.(string)
delete(opts, "var")
}
if opts["errors"] == nil && help.Value("errors") != nil {
opts["errors"] = help.Value("errors")
}
form := NewForm(opts, help)
// Run the options on the form
for _, optionFn := range formOptions {
optionFn(form)
}
help.Set(hn, form)
s, err := help.Block()
if err != nil {
return "", err
}
form.Append(s)
return form.HTML(), nil
}
}