Skip to content

Commit 8769eab

Browse files
committed
Test: Ignore elements that belong to other/nested forms
1 parent c8f8f7b commit 8769eab

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

test/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ <h2 id="qunit-userAgent"></h2>
113113
<input data-rule-greaterThan="#y1" value="10" name="y2" id="y2">
114114
<input data-rule-lessThanEqual="#z2" value="10" name="z1" id="z1">
115115
<input data-rule-greaterThanEqual="#z1" value="10" name="z2" id="z2">
116-
</form>
116+
</form>
117117
<form id="testForm6">
118118
<input data-rule-required="true" data-rule-minlength="2" type="checkbox" name="check" id="form6check1">
119119
<input type="checkbox" name="check" id="form6check2">
@@ -449,6 +449,9 @@ <h3></h3>
449449
<br>
450450
<input type="text" name="something" id="_something" />
451451
</form>
452+
<form id="testForm28">
453+
<input type="text" name="f28input" required>
454+
</form>
452455
</div>
453456
</body>
454457
</html>

test/test.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,173 @@ QUnit.test( "valid(), ignores ignored elements", function( assert ) {
187187
assert.ok( $( "#firstnamec" ).valid() );
188188
} );
189189

190+
QUnit.test( "valid() should ignore elements that belong to other/nested forms", function( assert ) {
191+
var form = $( "#testForm28" );
192+
193+
form.validate();
194+
195+
// 1. Test with nested form
196+
form.append(
197+
"<form id='testForm28-nested'>" +
198+
" <input type='text' name='f28nestedinput' required>" +
199+
"</form>"
200+
);
201+
202+
try {
203+
form.valid();
204+
assert.ok( true, "It should ignore the input of nested form" );
205+
} catch ( err ) {
206+
assert.ok( false, "Shouldn't throw an error" );
207+
}
208+
209+
// Remove the validator atteched to testForm28
210+
form.removeData( "validator" );
211+
$( "#testForm28-nested" ).remove();
212+
213+
// 2. Test using another form outside of validated one
214+
form.parent().append(
215+
"<form id='testForm28-other'>" +
216+
" <input type='text' name='f28otherinput' required>" +
217+
" <input type='text' name='f28input' form='testForm28' required>" +
218+
"</form>"
219+
);
220+
221+
$( "#testForm28-other" ).validate();
222+
223+
try {
224+
$( "#testForm28-other" ).valid();
225+
assert.ok( true, "It should ignore the input of other form" );
226+
} catch ( err ) {
227+
assert.ok( false, "Shouldn't throw an error" );
228+
}
229+
230+
$( "#testForm28-other" ).remove();
231+
} );
232+
233+
QUnit.test( "form() should ignore elements that belong to other/nested forms", function( assert ) {
234+
var form = $( "#testForm28" );
235+
var v = form.validate();
236+
237+
form.validate();
238+
239+
// 1. Test with nested form
240+
form.append(
241+
"<form id='testForm28-nested'>" +
242+
" <input type='text' name='f28nestedinput' required>" +
243+
"</form>"
244+
);
245+
246+
try {
247+
v.form();
248+
assert.ok( true, "It should ignore the input of nested form" );
249+
} catch ( err ) {
250+
assert.ok( false, "Shouldn't throw an error" );
251+
}
252+
253+
// Remove the validator atteched to testForm28
254+
form.removeData( "validator" );
255+
$( "#testForm28-nested" ).remove();
256+
257+
// 2. Test using another form outside of validated one
258+
form.parent().append(
259+
"<form id='testForm28-other'>" +
260+
" <input type='text' name='f28otherinput' required>" +
261+
" <input type='text' name='f28input' form='testForm28' required>" +
262+
"</form>"
263+
);
264+
265+
v = $( "#testForm28-other" ).validate();
266+
267+
try {
268+
v.form();
269+
assert.ok( true, "It should ignore the input of other form" );
270+
} catch ( err ) {
271+
assert.ok( false, "Shouldn't throw an error" );
272+
}
273+
274+
$( "#testForm28-other" ).remove();
275+
} );
276+
277+
QUnit.test( "elements() should ignore elements that belong to other/nested forms", function( assert ) {
278+
var form = $( "#testForm28" );
279+
var v = form.validate();
280+
281+
// 1. Test with nested form
282+
form.append(
283+
"<form id='testForm28-nested'>" +
284+
" <input type='text' name='f28nestedinput' required>" +
285+
"</form>"
286+
);
287+
288+
try {
289+
assert.equal( v.elements().length, 1, "There should be only one element to validate" );
290+
} catch ( err ) {
291+
assert.ok( false, "Shouldn't throw an error" );
292+
}
293+
294+
// Remove the validator atteched to testForm28
295+
form.removeData( "validator" );
296+
$( "#testForm28-nested" ).remove();
297+
298+
// 2. Test using another form outside of validated one
299+
form.parent().append(
300+
"<form id='testForm28-other'>" +
301+
" <input type='text' name='f28otherinput' required>" +
302+
" <input type='text' name='f28input' form='testForm28' required>" +
303+
"</form>"
304+
);
305+
306+
v = $( "#testForm28-other" ).validate();
307+
308+
try {
309+
assert.equal( v.elements().length, 1, "There should be only one element to validate" );
310+
} catch ( err ) {
311+
assert.ok( false, "Shouldn't throw an error" );
312+
}
313+
314+
$( "#testForm28-other" ).remove();
315+
} );
316+
317+
QUnit.test( "Ignore elements that have form attribute set to other forms", function( assert ) {
318+
319+
// Append a form that contains an input with form attribute set to "testForm28"
320+
$( "#testForm28" ).parent().append(
321+
"<form id='testForm28-other'>" +
322+
" <input type='text' name='f28otherinput' required>" +
323+
" <input type='text' name='f28input' form='testForm28' required>" +
324+
"</form>"
325+
);
326+
327+
// Attach the plugin to the appended form
328+
$( "#testForm28-other" ).validate();
329+
330+
// 1. Simulate typing
331+
try {
332+
$( "[name='f28input']", "#testForm28-other" ).trigger( "keyup" );
333+
assert.ok( true, "It should ignore the input of other form" );
334+
} catch ( err ) {
335+
assert.ok( false, "Shouldn't throw an error while typing" );
336+
}
337+
338+
// 2. Simulate forcussing in the input
339+
try {
340+
$( "[name='f28input']", "#testForm28-other" ).trigger( "focusin" );
341+
assert.ok( true, "It should ignore the input of other form" );
342+
} catch ( err ) {
343+
assert.ok( false, "Shouldn't throw an error on focus" );
344+
}
345+
346+
// 3. Simulate focussing out the input
347+
try {
348+
$( "[name='f28input']", "#testForm28-other" ).trigger( "focusout" );
349+
assert.ok( true, "It should ignore the input of other form" );
350+
} catch ( err ) {
351+
assert.ok( false, "Shouldn't throw an error on blur" );
352+
}
353+
354+
$( "#testForm28-other" ).remove();
355+
} );
356+
190357
QUnit.test( "addMethod", function( assert ) {
191358
assert.expect( 3 );
192359
$.validator.addMethod( "hi", function( value ) {

0 commit comments

Comments
 (0)