Skip to content

Commit

Permalink
Merge pull request #117 from think2011/master
Browse files Browse the repository at this point in the history
Fixed Issues #111
  • Loading branch information
Huei Tan committed Jun 4, 2015
2 parents 9cd9977 + f4cd192 commit fca3399
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
29 changes: 16 additions & 13 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,11 @@


/**
* If var is true, focus element when validate end
* @type {boolean}
* collect elements for focus
* @type {Object}
***private variable
*/
var isFocusElement = false;
var focusElements = {};


/**
Expand Down Expand Up @@ -520,7 +520,6 @@
*/
watch();

isFocusElement = false;
ctrl.$setViewValue('');
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
Expand All @@ -543,10 +542,6 @@
var value = ctrl.$viewValue,
isValid = false;

if (index === 0) {
isFocusElement = false;
}

isValid = checkValidation(scope, element, attrs, ctrl, validation, value);

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

}

// Focus first input element when submit error #11
if (!isFocusElement && !isValid) {
isFocusElement = true;
element[0].focus();
}
var setFocus = function(isValid) {
if(isValid) {
delete focusElements[index];
} else {
focusElements[index] = element[0];

$timeout(function() {
focusElements[Math.min.apply(null, Object.keys(focusElements))].focus();
}, 0);
}
};

isValid.constructor === Object ? isValid.then(setFocus) : setFocus(isValid);
});

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@


/**
* If var is true, focus element when validate end
* @type {boolean}
* collect elements for focus
* @type {Object}
***private variable
*/
var isFocusElement = false;
var focusElements = {};


/**
Expand Down Expand Up @@ -228,7 +228,6 @@
*/
watch();

isFocusElement = false;
ctrl.$setViewValue('');
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
Expand All @@ -251,10 +250,6 @@
var value = ctrl.$viewValue,
isValid = false;

if (index === 0) {
isFocusElement = false;
}

isValid = checkValidation(scope, element, attrs, ctrl, validation, value);

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

}

// Focus first input element when submit error #11
if (!isFocusElement && !isValid) {
isFocusElement = true;
element[0].focus();
}
var setFocus = function(isValid) {
if(isValid) {
delete focusElements[index];
} else {
focusElements[index] = element[0];

$timeout(function() {
focusElements[Math.min.apply(null, Object.keys(focusElements))].focus();
}, 0);
}
};

isValid.constructor === Object ? isValid.then(setFocus) : setFocus(isValid);
});

/**
Expand Down

0 comments on commit fca3399

Please sign in to comment.