registration.js
8.83 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
Ext.require([
'Ext.form.*',
'Ext.Img',
'Ext.tip.QuickTipManager'
]);
Ext.onReady(function() {
Ext.tip.QuickTipManager.init();
var formPanel = Ext.widget('form', {
renderTo: Ext.getBody(),
frame: true,
width: 350,
bodyPadding: 10,
bodyBorder: true,
title: 'Account Registration',
defaults: {
anchor: '100%'
},
fieldDefaults: {
labelAlign: 'left',
msgTarget: 'none',
invalidCls: '' //unset the invalidCls so individual fields do not get styled as invalid
},
/*
* Listen for validity change on the entire form and update the combined error icon
*/
listeners: {
fieldvaliditychange: function() {
this.updateErrorState();
},
fielderrorchange: function() {
this.updateErrorState();
}
},
updateErrorState: function() {
var me = this,
errorCmp, fields, errors;
if (me.hasBeenDirty || me.getForm().isDirty()) { //prevents showing global error when form first loads
errorCmp = me.down('#formErrorState');
fields = me.getForm().getFields();
errors = [];
fields.each(function(field) {
Ext.Array.forEach(field.getErrors(), function(error) {
errors.push({name: field.getFieldLabel(), error: error});
});
});
errorCmp.setErrors(errors);
me.hasBeenDirty = true;
}
},
items: [{
xtype: 'textfield',
name: 'username',
fieldLabel: 'User Name',
allowBlank: false,
minLength: 6
}, {
xtype: 'textfield',
name: 'email',
fieldLabel: 'Email Address',
vtype: 'email',
allowBlank: false
}, {
xtype: 'textfield',
name: 'password1',
fieldLabel: 'Password',
inputType: 'password',
style: 'margin-top:15px',
allowBlank: false,
minLength: 8
}, {
xtype: 'textfield',
name: 'password2',
fieldLabel: 'Repeat Password',
inputType: 'password',
allowBlank: false,
/**
* Custom validator implementation - checks that the value matches what was entered into
* the password1 field.
*/
validator: function(value) {
var password1 = this.previousSibling('[name=password1]');
return (value === password1.getValue()) ? true : 'Passwords do not match.'
}
},
/*
* Terms of Use acceptance checkbox. Two things are special about this:
* 1) The boxLabel contains a HTML link to the Terms of Use page; a special click listener opens this
* page in a modal Ext window for convenient viewing, and the Decline and Accept buttons in the window
* update the checkbox's state automatically.
* 2) This checkbox is required, i.e. the form will not be able to be submitted unless the user has
* checked the box. Ext does not have this type of validation built in for checkboxes, so we add a
* custom getErrors method implementation.
*/
{
xtype: 'checkboxfield',
name: 'acceptTerms',
fieldLabel: 'Terms of Use',
hideLabel: true,
style: 'margin-top:15px',
boxLabel: 'I have read and accept the <a href="#" class="terms">Terms of Use</a>.',
// Listener to open the Terms of Use page link in a modal window
listeners: {
click: {
element: 'boxLabelEl',
fn: function(e) {
var target = e.getTarget('.terms'),
win;
e.preventDefault();
if (target) {
win = Ext.widget('window', {
title: 'Terms of Use',
modal: true,
html: Ext.getDom('legalese').innerHTML,
width: 700,
height: 400,
bodyStyle: 'padding: 10px 20px;',
autoScroll: true,
buttons: [{
text: 'Decline',
handler: function() {
this.up('window').close();
formPanel.down('[name=acceptTerms]').setValue(false);
}
}, {
text: 'Accept',
handler: function() {
this.up('window').close();
formPanel.down('[name=acceptTerms]').setValue(true);
}
}]
});
win.show();
}
}
}
},
// Custom validation logic - requires the checkbox to be checked
getErrors: function() {
return this.getValue() ? [] : ['You must accept the Terms of Use']
}
}],
dockedItems: [{
xtype: 'container',
dock: 'bottom',
layout: {
type: 'hbox',
align: 'middle'
},
padding: '10 10 5',
items: [{
xtype: 'component',
id: 'formErrorState',
baseCls: 'form-error-state',
flex: 1,
validText: 'Form is valid',
invalidText: 'Form has errors',
tipTpl: Ext.create('Ext.XTemplate', '<ul><tpl for="."><li><span class="field-name">{name}</span>: <span class="error">{error}</span></li></tpl></ul>'),
getTip: function() {
var tip = this.tip;
if (!tip) {
tip = this.tip = Ext.widget('tooltip', {
target: this.el,
title: 'Error Details:',
autoHide: false,
anchor: 'top',
mouseOffset: [-11, -2],
closable: true,
constrainPosition: false,
cls: 'errors-tip'
});
tip.show();
}
return tip;
},
setErrors: function(errors) {
var me = this,
baseCls = me.baseCls,
tip = me.getTip();
errors = Ext.Array.from(errors);
// Update CSS class and tooltip content
if (errors.length) {
me.addCls(baseCls + '-invalid');
me.removeCls(baseCls + '-valid');
me.update(me.invalidText);
tip.setDisabled(false);
tip.update(me.tipTpl.apply(errors));
} else {
me.addCls(baseCls + '-valid');
me.removeCls(baseCls + '-invalid');
me.update(me.validText);
tip.setDisabled(true);
tip.hide();
}
}
}, {
xtype: 'button',
formBind: true,
disabled: true,
text: 'Submit Registration',
width: 140,
handler: function() {
var form = this.up('form').getForm();
/* Normally we would submit the form to the server here and handle the response...
form.submit({
clientValidation: true,
url: 'register.php',
success: function(form, action) {
//...
},
failure: function(form, action) {
//...
}
});
*/
if (form.isValid()) {
Ext.Msg.alert('Submitted Values', form.getValues(true));
}
}
}]
}]
});
});