-
Notifications
You must be signed in to change notification settings - Fork 2
/
field.go
117 lines (100 loc) · 2.87 KB
/
field.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
package forms
import (
"fmt"
"strings"
)
type Field struct {
// The field name
Name string
// The field label
Label string
// The field type ie text, textarea
Type string
// The default value
Default string
// The actual value
Value string
// Boolean flag for when a field has errors or not
Valid bool
// A list of values to use for multi-value inputs
Values []string
// A list of validation items to add to this field
Rules []Validation
// A list of errors on this field
Errors []string
// A Map of properties to add to the field output
Props map[string]string
}
// Creates a new field struct
func NewField() *Field {
return &Field{
Name: "Field",
Type: "text",
}
}
// Returns the string representation of a field, depending on its type
func (f Field) output() string {
switch f.Type {
case "textarea":
return f.outputTextArea()
case "radio":
case "checkbox":
return f.outputBox()
case "select":
return f.outputSelect()
}
return f.outputText()
}
// Adds an error to the field
func (f *Field) addError(err string) {
f.Errors = append(f.Errors, err)
}
// Adds a validation item to the field
func (f *Field) addValidation(validation Validation) {
f.Rules = append(f.Rules, validation)
}
// Runs all the validation associated with this field
func (f *Field) validate() {
f.Valid = true
for _, rule := range f.Rules {
rule.setValue(f.Value)
rule.validate()
if !rule.isValid() {
f.addError(rule.getErr())
f.Valid = false
}
}
}
// Returns the label for a field
func (f Field) label() string {
return fmt.Sprintf("<label for=\"%s\">%s</label>", f.Name, f.Label)
}
// Returns the string representation of a standard text input
func (f Field) outputText() string {
return fmt.Sprintf("<input type=\"%[1]s\" name=\"%[2]s\" id=\"%[2]s\" value=\"%[3]s\" />", f.Type, f.Name, f.Value)
}
// Returns the string representation of a textarea input
func (f Field) outputTextArea() string {
return fmt.Sprintf("<textarea name=\"%[1]s\" id=\"%[1]s\">%[2]s</textarea>", f.Name, f.Value)
}
// Returns the string representation of a checkbox/radio
// Currently, this is exactly the same as outputText, however there
// Will be some difference once request form values are added
func (f Field) outputBox() string {
return fmt.Sprintf("<input type=\"%[1]s\" name=\"%[2]s\" id=\"%[2]s\" value=\"%[3]s\" />", f.Type, f.Name, f.Value)
}
// Returns the string representation of a select box
func (f Field) outputSelect() string {
var options []string
for _, val := range f.Values {
options = append(options, fmt.Sprintf("<option value=\"%[1]s\">%[1]s</option>", val))
}
return fmt.Sprintf("<select name=\"%[1]s\" id=\"%[1]s\">%[2]s</select>", f.Name, strings.Join(options, ""))
}
// Returns a string representation of the classes property
func (f Field) getHtmlProperties() (html string) {
for key, val := range f.Props {
html += fmt.Sprintf("%s=\"%s\" ", key, val)
}
return
}