Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit 631b27c

Browse files
committed
Get form within scope
Previous code retrieved first form on page, and exited if that form did not have a name. This change will let it get first form with a name within scope. Useful if, like in my case, there are several forms on the page that are out of my control.
1 parent a961ef2 commit 631b27c

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/validation-common.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,20 @@ angular
283283
//----
284284
// Private functions declaration
285285
//----------------------------------
286-
286+
/** Get form within scope (if found)
287+
* @param self
288+
*/
289+
function getScopeForm(self) {
290+
var forms = document.querySelectorAll('form');
291+
for (var i = 0; i < forms.length; i++) {
292+
var form = document.querySelectorAll('form')[i];
293+
if (form && form.name && self.scope[form.name]) {
294+
return self.scope[form.name];
295+
}
296+
}
297+
return null;
298+
}
299+
287300
/** Add the error to the validation summary
288301
* @param self
289302
* @param string elmName: element name (name attribute)
@@ -309,10 +322,10 @@ angular
309322

310323
// save validation summary 2 variable locations, inside the scope object and also in the form object (if found)
311324
self.scope.$validationSummary = validationSummary;
312-
var formName = angular.element(document.querySelector('form')).attr('name');
313-
if(!!formName) {
314-
self.scope[formName].$validationSummary = validationSummary;
315-
}
325+
var form = getScopeForm(self);
326+
if (form) {
327+
form.$validationSummary = validationSummary;
328+
}
316329
}
317330

318331
/** Quick function to find an object inside an array by it's given field name and value, return the index found or -1

0 commit comments

Comments
 (0)