-
Notifications
You must be signed in to change notification settings - Fork 6
/
textbox_password.go
107 lines (93 loc) · 3.82 KB
/
textbox_password.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/page"
)
// PasswordTextboxI is the interface that defines a PasswordTextbox
type PasswordTextboxI interface {
TextboxI
}
// PasswordTextbox is a Textbox for passwords. It has the "password" type attribute, and it is specially
// controlled so that the password value is never stored in cleartext, either through the pagestate store
// or through the state store.
type PasswordTextbox struct {
Textbox
}
// NewPasswordTextbox creates a new PasswordTextbox
func NewPasswordTextbox(parent page.ControlI, id string) *PasswordTextbox {
t := &PasswordTextbox{}
t.Self = t
t.Init(parent, id)
return t
}
// Init is called by the framework to initialize the control. Only subclasses need to call it.
func (t *PasswordTextbox) Init(parent page.ControlI, id string) {
t.Textbox.Init(parent, id)
t.SetAttribute("autocomplete", "off")
t.SetType(TextboxTypePassword)
// Indicate to goradd.js to always post this ajax value
// We need this since we are not storing the value in the pagestate
t.SetDataAttribute("grPost", true)
}
// Serialize is used by the framework to serialize the textbox into the pagestate.
//
// This special override prevents the value of the password from ever getting put into the pagestate store.
func (t *PasswordTextbox) Serialize(e page.Encoder) {
t.value = ""
t.Textbox.Serialize(e)
return
}
// SaveState normally is used to save the text of the control to restore it if the page is returned to.
// This version panics, so that you never SaveState on a password text box.
func (t *PasswordTextbox) SaveState(_ context.Context, _ bool) {
panic("do not call SaveState on a password textbox as it would be a security risk")
}
// PasswordTextboxCreator creates a PasswordTextbox.
// Pass it to AddControls of a control, or as a Child of
// a FormFieldWrapper.
type PasswordTextboxCreator 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
// 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
// Text is the initial value of the textbox. Often it is best to load the value in a separate Load step after creating the control.
Text string
page.ControlOptions
}
// Create is called by the framework to turn the PasswordTextboxCreator into a control. You do not
// normally need to call it.
func (c PasswordTextboxCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewPasswordTextbox(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
// Init is called by the framework to initialize a newly created PasswordTextbox. You do not
// normally need to call it.
func (c PasswordTextboxCreator) Init(ctx context.Context, ctrl PasswordTextboxI) {
// Reuse subclass
sub := TextboxCreator{
Placeholder: c.Placeholder,
MinLength: c.MinLength,
MaxLength: c.MaxLength,
ColumnCount: c.ColumnCount,
ControlOptions: c.ControlOptions,
Text: c.Text,
}
sub.Init(ctx, ctrl)
}
// GetPasswordTextbox is a convenience method to return the control with the given id from the page.
func GetPasswordTextbox(c page.ControlI, id string) *PasswordTextbox {
return c.Page().GetControl(id).(*PasswordTextbox)
}
func init() {
page.RegisterControl(&PasswordTextbox{})
}