diff --git a/docs/content/error/ngModel/nopromise.ngdoc b/docs/content/error/ngModel/nopromise.ngdoc new file mode 100644 index 000000000000..ef75f602a4e1 --- /dev/null +++ b/docs/content/error/ngModel/nopromise.ngdoc @@ -0,0 +1,28 @@ +@ngdoc error +@name ngModel:nopromise +@fullName No promise +@description + +The return value of an async validator, must always be a promise. If you want to return a +non-promise value, you can convert it to a promise using {@link ng.$q#resolve `$q.resolve()`} or +{@link ng.$q#reject `$q.reject()`}. + +Example: + +``` +.directive('asyncValidator', function($q) { + return { + require: 'ngModel', + link: function(scope, elem, attrs, ngModel) { + ngModel.$asyncValidators.myAsyncValidation = function(modelValue, viewValue) { + if (/* I don't need to hit the backend API */) { + return $q.resolve(); // to mark as valid or + // return $q.reject(); // to mark as invalid + } else { + // ...send a request to the backend and return a promise + } + } + } + }; +}) +``` diff --git a/src/ng/directive/ngModel.js b/src/ng/directive/ngModel.js index 4683f4e31c4d..a00c1d8ae9b7 100644 --- a/src/ng/directive/ngModel.js +++ b/src/ng/directive/ngModel.js @@ -638,7 +638,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ forEach(ctrl.$asyncValidators, function(validator, name) { var promise = validator(modelValue, viewValue); if (!isPromiseLike(promise)) { - throw ngModelMinErr("$asyncValidators", + throw ngModelMinErr('nopromise', "Expected asynchronous validator to return a promise but got '{0}' instead.", promise); } setValidity(name, undefined); diff --git a/test/ng/directive/ngModelSpec.js b/test/ng/directive/ngModelSpec.js index e725d495fe00..2210ea01b9fd 100644 --- a/test/ng/directive/ngModelSpec.js +++ b/test/ng/directive/ngModelSpec.js @@ -906,7 +906,7 @@ describe('ngModel', function() { expect(function() { scope.$apply('value = "123"'); - }).toThrowMinErr("ngModel", "$asyncValidators", + }).toThrowMinErr("ngModel", "nopromise", "Expected asynchronous validator to return a promise but got 'true' instead."); }));