error-placement.js
10.5 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
module( "placement" );
test( "elements() order", function() {
var container = $( "#orderContainer" ),
v = $( "#elementsOrder" ).validate({
errorLabelContainer: container,
wrap: "li"
});
deepEqual(
v.elements().map( function() {
return $( this ).attr( "id" );
}).get(),
[
"order1",
"order2",
"order3",
"order4",
"order5",
"order6"
],
"elements must be in document order"
);
v.form();
deepEqual(
container.children().map( function() {
return $( this ).attr( "id" );
}).get(),
[
"order1-error",
"order2-error",
"order3-error",
"order4-error",
"order5-error",
"order6-error"
],
"labels in error container must be in document order"
);
});
test( "error containers, simple", function() {
expect( 14 );
var container = $( "#simplecontainer" ),
v = $( "#form" ).validate({
errorLabelContainer: container,
showErrors: function() {
container.find( "h3" ).html( jQuery.validator.format( "There are {0} errors in your form.", this.size()) );
this.defaultShowErrors();
}
});
v.prepareForm();
ok( v.valid(), "form is valid" );
equal( 0, container.find( ".error:not(input)" ).length, "There should be no error labels" );
equal( "", container.find( "h3" ).html() );
v.prepareForm();
v.errorList = [
{
message: "bar",
element: {
name: "foo"
}
},
{
message: "necessary",
element: {
name: "required"
}
}
];
ok( !v.valid(), "form is not valid after adding errors manually" );
v.showErrors();
equal( container.find( ".error:not(input)" ).length, 2, "There should be two error labels" );
ok( container.is( ":visible" ), "Check that the container is visible" );
container.find( ".error:not(input)" ).each(function() {
ok( $( this ).is( ":visible" ), "Check that each label is visible" );
});
equal( "There are 2 errors in your form.", container.find( "h3" ).html() );
v.prepareForm();
ok( v.valid(), "form is valid after a reset" );
v.showErrors();
equal( container.find( ".error:not(input)" ).length, 2, "There should still be two error labels" );
ok( container.is( ":hidden" ), "Check that the container is hidden" );
container.find( ".error:not(input)" ).each(function() {
ok( $( this ).is( ":hidden" ), "Check that each label is hidden" );
});
});
test( "error containers, with labelcontainer I", function() {
expect( 16 );
var container = $( "#container" ),
labelcontainer = $( "#labelcontainer" ),
v = $( "#form" ).validate({
errorContainer: container,
errorLabelContainer: labelcontainer,
wrapper: "li"
});
ok( v.valid(), "form is valid" );
equal( 0, container.find( ".error:not(input)" ).length, "There should be no error labels in the container" );
equal( 0, labelcontainer.find( ".error:not(input)" ).length, "There should be no error labels in the labelcontainer" );
equal( 0, labelcontainer.find( "li" ).length, "There should be no lis labels in the labelcontainer" );
v.errorList = [
{
message: "bar",
element: {
name: "foo"
}
},
{
name: "required",
message: "necessary",
element: {
name: "required"
}
}
];
ok( !v.valid(), "form is not valid after adding errors manually" );
v.showErrors();
equal( 0, container.find( ".error:not(input)" ).length, "There should be no error label in the container" );
equal( 2, labelcontainer.find( ".error:not(input)" ).length, "There should be two error labels in the labelcontainer" );
equal( 2, labelcontainer.find( "li" ).length, "There should be two error lis in the labelcontainer" );
ok( container.is( ":visible" ), "Check that the container is visible" );
ok( labelcontainer.is( ":visible" ), "Check that the labelcontainer is visible" );
labelcontainer.find( ".error:not(input)" ).each(function() {
ok( $( this ).is( ":visible" ), "Check that each label is visible1" );
equal( "li", $( this ).parent()[0].tagName.toLowerCase(), "Check that each label is wrapped in an li" );
ok( $( this ).parent( "li" ).is( ":visible" ), "Check that each parent li is visible" );
});
});
test( "errorcontainer, show/hide only on submit", function() {
expect( 14 );
var container = $( "#container" ),
labelContainer = $( "#labelcontainer" ),
v = $( "#testForm1" ).bind( "invalid-form.validate", function() {
ok( true, "invalid-form event triggered called" );
}).validate({
errorContainer: container,
errorLabelContainer: labelContainer,
showErrors: function() {
container.html( jQuery.validator.format( "There are {0} errors in your form.", this.numberOfInvalids()) );
ok( true, "showErrors called" );
this.defaultShowErrors();
}
});
equal( "", container.html(), "must be empty" );
equal( "", labelContainer.html(), "must be empty" );
// validate whole form, both showErrors and invalidHandler must be called once
// preferably invalidHandler first, showErrors second
ok( !v.form(), "invalid form" );
equal( 2, labelContainer.find( ".error:not(input)" ).length );
equal( "There are 2 errors in your form.", container.html() );
ok( labelContainer.is( ":visible" ), "must be visible" );
ok( container.is( ":visible" ), "must be visible" );
$( "#firstname" ).val( "hix" ).keyup();
$( "#testForm1" ).triggerHandler( "keyup", [
jQuery.event.fix({
type: "keyup",
target: $( "#firstname" )[ 0 ]
})
]);
equal( 1, labelContainer.find( ".error:visible" ).length );
equal( "There are 1 errors in your form.", container.html() );
$( "#lastname" ).val( "abc" );
ok( v.form(), "Form now valid, trigger showErrors but not invalid-form" );
});
test( "test label used as error container", function(assert) {
expect( 8 );
var form = $( "#testForm16" ),
field = $( "#testForm16text" );
form.validate({
errorPlacement: function( error, element ) {
// Append error within linked label
$( "label[for='" + element.attr( "id" ) + "']" ).append( error );
},
errorElement: "span"
});
ok( !field.valid() );
equal( "Field Label", field.next( "label" ).contents().first().text(), "container label isn't disrupted" );
assert.hasError(field, "missing");
ok( !field.attr( "aria-describedby" ), "field does not require aria-describedby attribute" );
field.val( "foo" );
ok( field.valid() );
equal( "Field Label", field.next( "label" ).contents().first().text(), "container label isn't disrupted" );
ok( !field.attr( "aria-describedby" ), "field does not require aria-describedby attribute" );
assert.noErrorFor(field);
});
test( "test error placed adjacent to descriptive label", function(assert) {
expect( 8 );
var form = $( "#testForm16" ),
field = $( "#testForm16text" );
form.validate({
errorElement: "span"
});
ok( !field.valid() );
equal( 1, form.find( "label" ).length );
equal( "Field Label", form.find( "label" ).text(), "container label isn't disrupted" );
assert.hasError( field, "missing" );
field.val( "foo" );
ok( field.valid() );
equal( 1, form.find( "label" ).length );
equal( "Field Label", form.find( "label" ).text(), "container label isn't disrupted" );
assert.noErrorFor( field );
});
test( "test descriptive label used alongside error label", function(assert) {
expect( 8 );
var form = $( "#testForm16" ),
field = $( "#testForm16text" );
form.validate({
errorElement: "label"
});
ok( !field.valid() );
equal( 1, form.find( "label.title" ).length );
equal( "Field Label", form.find( "label.title" ).text(), "container label isn't disrupted" );
assert.hasError( field, "missing" );
field.val( "foo" );
ok( field.valid() );
equal( 1, form.find( "label.title" ).length );
equal( "Field Label", form.find( "label.title" ).text(), "container label isn't disrupted" );
assert.noErrorFor( field );
});
test( "test custom errorElement", function(assert) {
expect( 4 );
var form = $( "#userForm" ),
field = $( "#username" );
form.validate({
messages: {
username: "missing"
},
errorElement: "label"
});
ok( !field.valid() );
assert.hasError( field, "missing", "Field should have error 'missing'" );
field.val( "foo" );
ok( field.valid() );
assert.noErrorFor( field, "Field should not have a visible error" );
});
test( "test existing label used as error element", function(assert) {
expect( 4 );
var form = $( "#testForm14" ),
field = $( "#testForm14text" );
form.validate({ errorElement: "label" });
ok( !field.valid() );
assert.hasError( field, "required" );
field.val( "foo" );
ok( field.valid() );
assert.noErrorFor( field );
});
test( "test existing non-label used as error element", function(assert) {
expect( 4 );
var form = $( "#testForm15" ),
field = $( "#testForm15text" );
form.validate({ errorElement: "span" });
ok( !field.valid() );
assert.hasError( field, "required" );
field.val( "foo" );
ok( field.valid() );
assert.noErrorFor( field );
});
test( "test existing non-error aria-describedby", function( assert ) {
expect( 8 );
var form = $( "#testForm17" ),
field = $( "#testForm17text" );
equal( field.attr( "aria-describedby" ), "testForm17text-description" );
form.validate({ errorElement: "span" });
ok( !field.valid() );
equal( field.attr( "aria-describedby" ), "testForm17text-description testForm17text-error" );
assert.hasError( field, "required" );
field.val( "foo" );
ok( field.valid() );
assert.noErrorFor( field );
strictEqual( "This is where you enter your data", $("#testForm17text-description").text() );
strictEqual( "", $("#testForm17text-error").text(), "Error label is empty for valid field" );
});
test( "test pre-assigned non-error aria-describedby", function( assert ) {
expect( 7 );
var form = $( "#testForm17" ),
field = $( "#testForm17text" );
// Pre-assign error identifier
field.attr( "aria-describedby", "testForm17text-description testForm17text-error" );
form.validate({ errorElement: "span" });
ok( !field.valid() );
equal( field.attr( "aria-describedby" ), "testForm17text-description testForm17text-error" );
assert.hasError( field, "required" );
field.val( "foo" );
ok( field.valid() );
assert.noErrorFor( field );
strictEqual( "This is where you enter your data", $("#testForm17text-description").text() );
strictEqual( "", $("#testForm17text-error").text(), "Error label is empty for valid field" );
});
test( "test id/name containing brackets", function( assert ) {
var form = $( "#testForm18" ),
field = $( "#testForm18\\[text\\]" );
form.validate({
errorElement: "span"
});
form.valid();
field.valid();
assert.hasError( field, "required" );
});
test( "test id/name containing $", function( assert ) {
var form = $( "#testForm19" ),
field = $( "#testForm19\\$text" );
form.validate({
errorElement: "span"
});
field.valid();
assert.hasError( field, "required" );
});