Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(input): $asyncValidators should allow $parsers
Browse files Browse the repository at this point in the history
After resolving the async validators, it should execute all parsers
on the viewValue before comparing it to the currentValue.
  • Loading branch information
Gonzalo Ruiz de Villa committed Sep 2, 2014
1 parent 203ea10 commit 2fed968
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
$animate.removeClass($element, PENDING_CLASS);
};

this.$$setPending = function(validationErrorKey, promise, currentValue) {
this.$$setPending = function(validationErrorKey, promise, modelValue, viewValue) {
ctrl.$pending = ctrl.$pending || {};
if (angular.isUndefined(ctrl.$pending[validationErrorKey])) {
ctrl.$pending[validationErrorKey] = true;
Expand All @@ -1725,19 +1725,20 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$

//Special-case for (undefined|null|false|NaN) values to avoid
//having to compare each of them with each other
currentValue = currentValue || '';
viewValue = viewValue || '';
promise.then(resolve(true), resolve(false));

function resolve(bool) {
return function() {
var value = ctrl.$viewValue || '';
if (ctrl.$pending && ctrl.$pending[validationErrorKey] && currentValue === value) {
var value = ctrl.$viewValue;
value = value || '';
if (ctrl.$pending && ctrl.$pending[validationErrorKey] && viewValue === value) {
pendingCount--;
delete ctrl.$pending[validationErrorKey];
ctrl.$setValidity(validationErrorKey, bool);
if (pendingCount === 0) {
ctrl.$$clearPending();
ctrl.$$updateValidModelValue(value);
ctrl.$$updateValidModelValue(modelValue);
ctrl.$$writeModelToScope();
}
}
Expand Down Expand Up @@ -1947,7 +1948,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
throw $ngModelMinErr("$asyncValidators",
"Expected asynchronous validator to return a promise but got '{0}' instead.", result);
}
ctrl.$$setPending(validator, result, modelValue);
ctrl.$$setPending(validator, result, modelValue, viewValue);
});
}

Expand Down
25 changes: 25 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,31 @@ describe('NgModelController', function() {
dealoc(element);
}));

it('should re-evaluate the form validity state against a parsed view value',
inject(function($compile, $rootScope, $q) {
var element = $compile('<form name="myForm">' +
'<input type="number" name="curiousnumber" ng-model="curiousnumber" />' +
'</form>')($rootScope);
var inputElm = element.find('input');

var formCtrl = $rootScope.myForm;
var curiousnumberCtrl = formCtrl.curiousnumber;
var curiousnumberDefer;
curiousnumberCtrl.$asyncValidators.isCurious = function() {
curiousnumberDefer = $q.defer();
return curiousnumberDefer.promise;
};

curiousnumberCtrl.$setViewValue("22");
$rootScope.$digest();
expect(curiousnumberCtrl.$pending.isCurious).toBe(true);

curiousnumberDefer.resolve();
$rootScope.$digest();
expect(curiousnumberCtrl.$pending).toBeUndefined();

dealoc(element);
}));
});
});

Expand Down

0 comments on commit 2fed968

Please sign in to comment.