-
Notifications
You must be signed in to change notification settings - Fork 6
/
textbox_email.go
199 lines (176 loc) · 5.79 KB
/
textbox_email.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package control
import (
"context"
"fmt"
"github.com/goradd/goradd/pkg/page"
"net/mail"
)
type EmailTextboxI interface {
TextboxI
SetMaxItemCount(count int) EmailTextboxI
}
// EmailTextbox is a Textbox control that validates for email addresses.
// EmailTextbox can accept multiple addresses separated by commas, and can accept any address in RFC5322 format (Barry Gibbs <bg@example.com>)
// making it useful for people copying addresses out of an email client and pasting into the field.
type EmailTextbox struct {
Textbox
maxItemCount int
items []*mail.Address
parseErr string
}
// NewEmailTextbox creates a new textbox that validates its input as an email address.
// multi will allow the textbox to accept multiple email addresses separated by a comma.
func NewEmailTextbox(parent page.ControlI, id string) *EmailTextbox {
t := &EmailTextbox{}
t.Self = t
t.Init(parent, id)
return t
}
func (t *EmailTextbox) Init(parent page.ControlI, id string) {
t.Textbox.Init(parent, id)
t.maxItemCount = 1
t.SetType(TextboxTypeEmail)
}
func (t *EmailTextbox) this() EmailTextboxI {
return t.Self.(EmailTextboxI)
}
func (t *EmailTextbox) SetMaxItemCount(max int) EmailTextboxI {
t.maxItemCount = max
if t.maxItemCount > 1 {
t.SetType(TextboxTypeDefault) // Some browsers cannot handle multiple emails in an email type of text input
}
t.Refresh()
return t.this()
}
func (t *EmailTextbox) Validate(ctx context.Context) bool {
ret := t.Textbox.Validate(ctx)
if ret {
if t.parseErr != "" {
t.SetValidationError(t.GT("Not a valid email address: " + t.parseErr))
return false
} else if len(t.items) > t.maxItemCount {
if t.maxItemCount == 1 {
t.SetValidationError(t.GT("Enter only one email address"))
} else {
t.SetValidationError(fmt.Sprintf(t.GT("Enter at most %d email addresses separated by commas"), t.maxItemCount))
}
return false
}
}
return ret
}
// UpdateFormValues is used by the framework to cause the control to retrieve its values from the form
func (t *EmailTextbox) UpdateFormValues(ctx context.Context) {
t.Textbox.UpdateFormValues(ctx)
if t.Text() == "" {
t.items = nil
t.parseErr = ""
return
}
var parseErr error
t.items, parseErr = mail.ParseAddressList(t.Text())
if parseErr != nil {
t.parseErr = parseErr.Error()
} else {
t.parseErr = ""
}
}
// Addresses returns a slice of the individual addresses entered, stripped of any extra text entered.
func (t *EmailTextbox) Addresses() (ret []string) {
for _, item := range t.items {
ret = append(ret, item.Address)
}
return ret
}
func (t *EmailTextbox) Serialize(e page.Encoder) (err error) {
if err = t.Textbox.Serialize(e); err != nil {
panic(err)
}
if err = e.Encode(t.maxItemCount); err != nil {
panic(err)
}
if err = e.Encode(t.items); err != nil {
panic(err)
}
if err = e.Encode(t.parseErr); err != nil {
panic(err)
}
return
}
func (t *EmailTextbox) Deserialize(dec page.Decoder) (err error) {
if err = t.Textbox.Deserialize(dec); err != nil {
return
}
if err = dec.Decode(&t.maxItemCount); err != nil {
return
}
if err = dec.Decode(&t.items); err != nil {
return
}
if err = dec.Decode(&t.parseErr); err != nil {
return
}
return
}
// Use EmailTextboxCreator to create an email textbox.
// Pass it to AddControls of a control, or as a Child of
// a FormFieldWrapper.
type EmailTextboxCreator struct {
// ID is the control id of the html widget and must be unique to the page
ID string
// Placeholder is the placeholder attribute of the textbox and shows as help text inside the field
Placeholder string
// Type is the type attribute of the textbox
Type string
// MinLength is the minimum number of characters that the user is required to enter. If the
// length is less than this number, a validation error will be shown.
MinLength int
// MaxLength is the maximum number of characters that the user is required to enter. If the
// length is more than this number, a validation error will be shown.
MaxLength int
// ColumnCount is the number of characters wide the textbox will be, and becomes the width attribute in the tag.
// The actual width is browser dependent. For better control, use a width style property.
ColumnCount int
// ReadOnly sets the readonly attribute of the textbox, which prevents it from being changed by the user.
ReadOnly bool
// SaveState will save the text in the textbox, to be restored if the user comes back to the page.
// It is particularly helpful when the textbox is being used to filter the results of a query, so that
// when the user comes back to the page, he does not have to type the filter text again.
SaveState bool
// Text is the initial value of the textbox. Often its best to load the value in a separate Load step after creating the control.
Text string
// MaxItemCount is the maximum number of email addresses allowed to be entered, separated by commas
// By default it allows only 1.
MaxItemCount int
page.ControlOptions
}
func (c EmailTextboxCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewEmailTextbox(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
func (c EmailTextboxCreator) Init(ctx context.Context, ctrl EmailTextboxI) {
if c.MaxItemCount > 1 {
ctrl.SetMaxItemCount(c.MaxItemCount)
}
// Reuse subclass
sub := TextboxCreator{
Placeholder: c.Placeholder,
Type: c.Type,
MinLength: c.MinLength,
MaxLength: c.MaxLength,
ColumnCount: c.ColumnCount,
ReadOnly: c.ReadOnly,
ControlOptions: c.ControlOptions,
SaveState: c.SaveState,
Text: c.Text,
}
sub.Init(ctx, ctrl)
}
// GetEmailTextbox is a convenience method to return the control with the given id from the page.
func GetEmailTextbox(c page.ControlI, id string) *EmailTextbox {
return c.Page().GetControl(id).(*EmailTextbox)
}
func init() {
page.RegisterControl(&EmailTextbox{})
}