Skip to content

Commit fca3399

Browse files
author
Huei Tan
committed
Merge pull request #117 from think2011/master
Fixed Issues #111
2 parents 9cd9977 + f4cd192 commit fca3399

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

dist/angular-validation.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@
364364

365365

366366
/**
367-
* If var is true, focus element when validate end
368-
* @type {boolean}
367+
* collect elements for focus
368+
* @type {Object}
369369
***private variable
370370
*/
371-
var isFocusElement = false;
371+
var focusElements = {};
372372

373373

374374
/**
@@ -520,7 +520,6 @@
520520
*/
521521
watch();
522522

523-
isFocusElement = false;
524523
ctrl.$setViewValue('');
525524
ctrl.$setPristine();
526525
ctrl.$setValidity(ctrl.$name, undefined);
@@ -543,10 +542,6 @@
543542
var value = ctrl.$viewValue,
544543
isValid = false;
545544

546-
if (index === 0) {
547-
isFocusElement = false;
548-
}
549-
550545
isValid = checkValidation(scope, element, attrs, ctrl, validation, value);
551546

552547
if (attrs.validMethod === 'submit') {
@@ -570,11 +565,19 @@
570565

571566
}
572567

573-
// Focus first input element when submit error #11
574-
if (!isFocusElement && !isValid) {
575-
isFocusElement = true;
576-
element[0].focus();
577-
}
568+
var setFocus = function(isValid) {
569+
if(isValid) {
570+
delete focusElements[index];
571+
} else {
572+
focusElements[index] = element[0];
573+
574+
$timeout(function() {
575+
focusElements[Math.min.apply(null, Object.keys(focusElements))].focus();
576+
}, 0);
577+
}
578+
};
579+
580+
isValid.constructor === Object ? isValid.then(setFocus) : setFocus(isValid);
578581
});
579582

580583
/**

dist/angular-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directive.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@
7272

7373

7474
/**
75-
* If var is true, focus element when validate end
76-
* @type {boolean}
75+
* collect elements for focus
76+
* @type {Object}
7777
***private variable
7878
*/
79-
var isFocusElement = false;
79+
var focusElements = {};
8080

8181

8282
/**
@@ -228,7 +228,6 @@
228228
*/
229229
watch();
230230

231-
isFocusElement = false;
232231
ctrl.$setViewValue('');
233232
ctrl.$setPristine();
234233
ctrl.$setValidity(ctrl.$name, undefined);
@@ -251,10 +250,6 @@
251250
var value = ctrl.$viewValue,
252251
isValid = false;
253252

254-
if (index === 0) {
255-
isFocusElement = false;
256-
}
257-
258253
isValid = checkValidation(scope, element, attrs, ctrl, validation, value);
259254

260255
if (attrs.validMethod === 'submit') {
@@ -278,11 +273,19 @@
278273

279274
}
280275

281-
// Focus first input element when submit error #11
282-
if (!isFocusElement && !isValid) {
283-
isFocusElement = true;
284-
element[0].focus();
285-
}
276+
var setFocus = function(isValid) {
277+
if(isValid) {
278+
delete focusElements[index];
279+
} else {
280+
focusElements[index] = element[0];
281+
282+
$timeout(function() {
283+
focusElements[Math.min.apply(null, Object.keys(focusElements))].focus();
284+
}, 0);
285+
}
286+
};
287+
288+
isValid.constructor === Object ? isValid.then(setFocus) : setFocus(isValid);
286289
});
287290

288291
/**

0 commit comments

Comments
 (0)