From 48d03455f016d5652676a6f6407e96c324d248c2 Mon Sep 17 00:00:00 2001 From: ruado1987 Date: Sat, 23 Mar 2013 14:14:53 +0800 Subject: [PATCH] Fixes issue 401 which causes error when nested form created programatically by code or third-party library --- jquery.validate.js | 5 +++++ test/index.html | 4 ++++ test/test.js | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/jquery.validate.js b/jquery.validate.js index c86bf6abe..d94cd25ff 100644 --- a/jquery.validate.js +++ b/jquery.validate.js @@ -480,6 +480,11 @@ $.extend($.validator, { console.error( "%o has no name assigned", this); } + // ignore elements of nested forms + if ( this.form !== validator.currentForm ) { + return false; + } + // select only the first element for each name, and only those with rules specified if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) { return false; diff --git a/test/index.html b/test/index.html index bb9fc4ad8..52ceecf6e 100644 --- a/test/index.html +++ b/test/index.html @@ -318,6 +318,10 @@

+ +
+ +
diff --git a/test/test.js b/test/test.js index cdfa3edb2..5571aa0a6 100644 --- a/test/test.js +++ b/test/test.js @@ -841,6 +841,18 @@ test('bypassing validation on form submission',function () { equal($v.numberOfInvalids(), 1, "Validation failed correctly"); }); +test("elements() should ignore elements of nested forms", function() { + var validator, + $form = $( "#enclosingForm" ); + + $( "
" ).appendTo( $form ); + $( "
" ).appendTo( $form ); + $form[ 0 ].uname.value = "Test"; + + validator = $form.validate(); + + equal( validator.elements().length, 1, "Elements of nested forms are ignored" ); +}); module("misc");