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

Commit b24319c

Browse files
author
Gonzalo Ruiz de Villa
committed
fix(input): $asyncValidators should allow $parsers
After resolving the async validators, it should execute all parsers on the viewValue before comparing it to the currentValue.
1 parent 203ea10 commit b24319c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/ng/directive/input.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17301730

17311731
function resolve(bool) {
17321732
return function() {
1733-
var value = ctrl.$viewValue || '';
1733+
var value = ctrl.$viewValue;
1734+
for(var i = 0; i < ctrl.$parsers.length; i++) {
1735+
value = ctrl.$parsers[i](value);
1736+
if(isUndefined(value)) {
1737+
break;
1738+
}
1739+
}
1740+
value = value || '';
17341741
if (ctrl.$pending && ctrl.$pending[validationErrorKey] && currentValue === value) {
17351742
pendingCount--;
17361743
delete ctrl.$pending[validationErrorKey];

test/ng/directive/inputSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,37 @@ describe('NgModelController', function() {
730730
dealoc(element);
731731
}));
732732

733+
it('should re-evaluate the form validity state against a parsed view value',
734+
inject(function($compile, $rootScope, $q) {
735+
var element = $compile('<form name="myForm">' +
736+
'<input type="number" name="curiousnumber" ng-model="curiousnumber" />' +
737+
'</form>')($rootScope);
738+
var inputElm = element.find('input');
739+
740+
var formCtrl = $rootScope.myForm;
741+
var curiousnumberCtrl = formCtrl.curiousnumber;
742+
var curiousnumberDefer;
743+
curiousnumberCtrl.$asyncValidators.isCurious = function() {
744+
curiousnumberDefer = $q.defer();
745+
return curiousnumberDefer.promise;
746+
};
747+
748+
curiousnumberCtrl.$setViewValue("22");
749+
$rootScope.$digest();
750+
expect(curiousnumberCtrl.$pending.isCurious).toBe(true);
751+
752+
curiousnumberDefer.resolve();
753+
$rootScope.$digest();
754+
expect(curiousnumberCtrl.$pending).toBeUndefined();
755+
756+
/*expect(formCtrl.$invalid).toBe(true);
757+
758+
$rootScope.$digest();
759+
expect(curiousnumberCtrl.$invalid).toBe(false);
760+
expect(formCtrl.$invalid).toBe(false);
761+
*/
762+
dealoc(element);
763+
}));
733764
});
734765
});
735766

0 commit comments

Comments
 (0)