forked from bluele/gforms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
textfield.go
58 lines (50 loc) · 1.09 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
package gforms
import (
"reflect"
)
// It maps value to FormInstance.CleanedData as type `string`.
type TextField struct {
BaseField
}
// Create a new TextField instance.
func (f *TextField) New() FieldInterface {
fi := new(TextFieldInstance)
fi.Model = f
fi.V = nilV("")
return fi
}
// Instance for TextField
type TextFieldInstance struct {
FieldInstance
}
// Create a new TextField with validators and widgets.
func NewTextField(name string, vs Validators, ws ...Widget) *TextField {
f := new(TextField)
f.name = name
f.validators = vs
if len(ws) > 0 {
f.widget = ws[0]
}
return f
}
// Get a value from request data, and clean it as type `string`.
func (f *TextFieldInstance) Clean(data Data) error {
m, hasField := data[f.Model.GetName()]
if hasField {
f.V = m
v := m.rawValueAsString()
m.Kind = reflect.String
if v != nil {
m.Value = *v
m.IsNil = false
}
}
return nil
}
func (f *TextFieldInstance) html() string {
return renderTemplate("TextTypeField", newTemplateContext(f))
}
// Get as HTML format.
func (f *TextFieldInstance) Html() string {
return fieldToHtml(f)
}