This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
fields.go
160 lines (136 loc) · 2.73 KB
/
fields.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
154
155
156
157
158
159
160
package gforms
type Field interface {
New() FieldInterface
// Get field name
GetName() string
GetWidget() Widget
GetValidators() Validators
}
type Fields struct {
list []Field
nameMap map[string]Field
}
// Get ordered list for field object.
func (fs *Fields) List() []Field {
return fs.list
}
// Get field by name.
func (fs *Fields) Get(name string) (Field, bool) {
v, ok := fs.nameMap[name]
return v, ok
}
func (fs *Fields) AddField(field Field) bool {
name := field.GetName()
_, exists := fs.Get(name)
if !exists {
fs.list = append(fs.list, field)
fs.nameMap[name] = field
return true
}
return false
}
func NewFields(fields ...Field) *Fields {
fs := Fields{}
fs.nameMap = map[string]Field{}
for _, field := range fields {
fs.nameMap[field.GetName()] = field
}
fs.list = fields
return &fs
}
type BaseField struct {
name string
validators Validators
widget Widget
Field
}
func (f *BaseField) GetName() string {
return f.name
}
func (f *BaseField) GetWidget() Widget {
return f.widget
}
func (f *BaseField) GetValidators() Validators {
return f.validators
}
type FieldInterface interface {
GetModel() Field
GetName() string
GetV() *V
GetWidget() Widget
SetInitial(string)
Clean(Data) error
Validate(*FormInstance) []string
Html() string
html() string
Errors() []string
SetErrors([]string)
HasError() bool
}
type FieldInstance struct {
Model Field
errors []string
V *V
FieldInterface
}
func (f *FieldInstance) GetModel() Field {
return f.Model
}
func (f *FieldInstance) GetName() string {
return f.Model.GetName()
}
func (f *FieldInstance) GetWidget() Widget {
return f.Model.GetWidget()
}
func (f *FieldInstance) GetV() *V {
return f.V
}
func (f *FieldInstance) Errors() []string {
return f.errors
}
func (f *FieldInstance) SetErrors(errs []string) {
f.errors = errs
}
func (f *FieldInstance) HasError() bool {
return len(f.errors) != 0
}
func (f *FieldInstance) SetInitial(v string) {
f.V.RawStr = v
}
func (f *FieldInstance) Validate(fo *FormInstance) []string {
vs := f.Model.GetValidators()
if vs == nil {
return nil
}
var errs []string
for _, v := range vs {
err := v.Validate(f, fo)
if err != nil {
errs = append(errs, err.Error())
}
}
return errs
}
type FieldInterfaces struct {
list []FieldInterface
nameMap map[string]FieldInterface
}
func newFieldInterfaces(fs *Fields) *FieldInterfaces {
fis := new(FieldInterfaces)
fis.list = []FieldInterface{}
fis.nameMap = map[string]FieldInterface{}
for _, f := range fs.list {
nf := f.New()
fis.nameMap[f.GetName()] = nf
fis.list = append(fis.list, nf)
}
return fis
}
func fieldToHtml(f FieldInterface) string {
gd := f.GetModel().GetWidget()
if gd == nil {
return f.html()
} else {
return gd.html(f)
}
}