forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.go
70 lines (62 loc) · 2.16 KB
/
forms.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
package plush
import (
"fmt"
"html/template"
"github.com/gobuffalo/tags"
"github.com/gobuffalo/tags/form"
"github.com/gobuffalo/tags/form/bootstrap"
)
// FormHelper implements a Plush helper around the
// form.New function in the github.com/gobuffalo/tags/form package
func FormHelper(opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return form.New(opts)
})
}
// FormForHelper implements a Plush helper around the
// form.NewFormFor function in the github.com/gobuffalo/tags/form package
func FormForHelper(model interface{}, opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return form.NewFormFor(model, opts)
})
}
// BootstrapFormHelper implements a Plush helper around the
// bootstrap.New function in the github.com/gobuffalo/tags/form/bootstrap package
func BootstrapFormHelper(opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return bootstrap.New(opts)
})
}
// BootstrapFormForHelper implements a Plush helper around the
// bootstrap.NewFormFor function in the github.com/gobuffalo/tags/form/bootstrap package
func BootstrapFormForHelper(model interface{}, opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return bootstrap.NewFormFor(model, opts)
})
}
type helperable interface {
SetAuthenticityToken(string)
Append(...tags.Body)
HTMLer
}
func helper(opts tags.Options, help HelperContext, fn func(opts tags.Options) helperable) (template.HTML, error) {
hn := "f"
if n, ok := opts["var"]; ok {
hn = n.(string)
delete(opts, "var")
}
if opts["errors"] == nil && help.Context.Value("errors") != nil {
opts["errors"] = help.Context.Value("errors")
}
form := fn(opts)
if help.Value("authenticity_token") != nil && opts["method"] != "GET" {
form.SetAuthenticityToken(fmt.Sprint(help.Value("authenticity_token")))
}
help.Context.Set(hn, form)
s, err := help.Block()
if err != nil {
return "", err
}
form.Append(s)
return form.HTML(), nil
}