-
Notifications
You must be signed in to change notification settings - Fork 6
/
textbox_integer.go
139 lines (120 loc) · 3.34 KB
/
textbox_integer.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
package control
import (
"fmt"
"github.com/goradd/goradd/pkg/page"
"strconv"
)
type IntegerTextbox struct {
Textbox
minValue *int
maxValue *int
}
func NewIntegerTextbox(parent page.ControlI, id string) *IntegerTextbox {
t := &IntegerTextbox{}
t.Init(t, parent, id)
return t
}
func (i *IntegerTextbox) Init(self TextboxI, parent page.ControlI, id string) {
i.Textbox.Init(self, parent, id)
i.ValidateWith(IntValidator{})
}
// SetMinValue creates a validator that makes sure the value of the text box is at least the
// given value. Specify your own error message, or leave the error message blank and a standard error message will
// be presented if the value is not valid.
func (i *IntegerTextbox) SetMinValue(minValue int, invalidMessage string) {
i.ValidateWith(MinIntValidator{minValue, invalidMessage})
i.minValue = new(int)
*i.minValue = minValue
}
// SetMaxValue creates a validator that makes sure the value of the text box is at most the
// given value. Specify your own error message, or leave the error message blank and a standard error message will
// be presented if the value is not valid.
func (i *IntegerTextbox) SetMaxValue(maxValue int, invalidMessage string) {
i.ValidateWith(MaxIntValidator{maxValue, invalidMessage})
i.maxValue = new(int)
*i.maxValue = maxValue
}
func (i *IntegerTextbox) SetValue(v interface{}) *IntegerTextbox {
i.Textbox.SetValue(v)
newValue := i.Int()
if i.minValue != nil && *i.minValue > newValue {
panic("Setting IntegerTextbox to a value less than minimum value.")
}
if i.maxValue != nil && *i.maxValue < newValue {
panic("Setting IntegerTextbox to a value greater than the maximum value.")
}
return i
}
func (i *IntegerTextbox) SetInt(v int) *IntegerTextbox {
i.Textbox.SetValue(v)
if i.minValue != nil && *i.minValue > v {
panic("Setting IntegerTextbox to a value less than minimum value.")
}
if i.maxValue != nil && *i.maxValue < v {
panic("Setting IntegerTextbox to a value greater than the maximum value.")
}
return i
}
func (i *IntegerTextbox) Value() interface{} {
return i.Int()
}
func (i *IntegerTextbox) Int() int {
t := i.Textbox.Text()
v, _ := strconv.Atoi(t)
return v
}
func (i *IntegerTextbox) Int64() int64 {
t := i.Textbox.Text()
i64, _ := strconv.ParseInt(t, 10, 0)
return i64
}
type IntValidator struct {
Message string
}
func (v IntValidator) Validate(c page.ControlI, s string) (msg string) {
if s == "" {
return "" // empty textbox is checked elsewhere
}
if _, err := strconv.Atoi(s); err != nil {
if v.Message == "" {
return c.T("Please enter an integer.")
} else {
return v.Message
}
}
return
}
type MinIntValidator struct {
MinValue int
Message string
}
func (v MinIntValidator) Validate(c page.ControlI, s string) (msg string) {
if s == "" {
return "" // empty textbox is checked elsewhere
}
if val, _ := strconv.Atoi(s); val < v.MinValue {
if v.Message == "" {
return fmt.Sprintf(c.ΩT("Enter at least %d"), v.MinValue)
} else {
return v.Message
}
}
return
}
type MaxIntValidator struct {
MaxValue int
Message string
}
func (v MaxIntValidator) Validate(c page.ControlI, s string) (msg string) {
if s == "" {
return "" // empty textbox is checked elsewhere
}
if val, _ := strconv.Atoi(s); val < v.MaxValue {
if v.Message == "" {
return fmt.Sprintf(c.ΩT("Enter at most %d"), v.MaxValue)
} else {
return v.Message
}
}
return
}