-
Notifications
You must be signed in to change notification settings - Fork 0
/
textfield.go
113 lines (96 loc) · 2.59 KB
/
textfield.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
package fork
import (
"fmt"
"net/http"
"net/mail"
)
func newtextField(name string, w Widget, v Validater, f Filterer) Field {
return &textField{
named: newnamed(name),
Processor: NewProcessor(w, v, f),
}
}
type textField struct {
Text string
*named
Processor
}
func (t *textField) New(i ...interface{}) Field {
var newfield textField = *t
newfield.named = t.named.Copy()
newfield.Text = ""
newfield.SetValidateable(false)
return &newfield
}
func (t *textField) Get() *Value {
return NewValue(t.Text)
}
func (t *textField) Set(r *http.Request) {
v := t.Filter(t.Name(), r)
t.Text = v.String()
t.SetValidateable(true)
}
func textWidget(options ...string) Widget {
return NewWidget(WithOptions(`<input type="text" name="{{ .Name }}" value="{{ .Text }}" %s>`, options...))
}
func TextField(name string, v []interface{}, f []interface{}, options ...string) Field {
return newtextField(
name,
textWidget(options...),
NewValidater(v...),
NewFilterer(f...),
)
}
func textAreaWidget(options ...string) Widget {
return NewWidget(WithOptions(`<textarea name="{{ .Name }}" %s>{{ .Text }}</textarea>`, options...))
}
func TextAreaField(name string, v []interface{}, f []interface{}, options ...string) Field {
return newtextField(
name,
textAreaWidget(options...),
NewValidater(v...),
NewFilterer(f...),
)
}
func hiddenWidget(options ...string) Widget {
return NewWidget(WithOptions(`<input type="hidden" name="{{ .Name }}" value="{{ .Text }}" %s>`, options...))
}
func HiddenField(name string, v []interface{}, f []interface{}, options ...string) Field {
return newtextField(
name,
hiddenWidget(options...),
NewValidater(v...),
NewFilterer(f...),
)
}
func passwordWidget(options ...string) Widget {
return NewWidget(WithOptions(`<input type="password" name="{{ .Name }}" value="{{ .Text }}" %s>`, options...))
}
func PassWordField(name string, v []interface{}, f []interface{}, options ...string) Field {
return newtextField(
name,
passwordWidget(options...),
NewValidater(v...),
NewFilterer(f...),
)
}
func emailWidget(options ...string) Widget {
return NewWidget(WithOptions(`<input type="email" name="{{ .Name }}" value="{{ .Text }}" %s>`, options...))
}
func EmailField(name string, v []interface{}, f []interface{}, options ...string) Field {
return newtextField(
name,
emailWidget(options...),
NewValidater(append(v, ValidEmail)...),
NewFilterer(f...),
)
}
func ValidEmail(t *textField) error {
if t.Validateable() {
_, err := mail.ParseAddress(t.Text)
if err != nil {
return fmt.Errorf("Invalid email address: %s", err.Error())
}
}
return nil
}