From 7df9181e85e0c5f0bb4614dbd8cdfe7f2814e2d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Wed, 30 Apr 2014 20:56:50 -0400 Subject: [PATCH] fix(NgModel): make sure the required validator uses the $validators pipeline Fixes #5164 --- src/ng/directive/input.js | 16 ++++------------ test/ng/directive/inputSpec.js | 2 +- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index e3701ee484b7..f9d024663e18 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1786,6 +1786,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ */ this.$commitViewValue = function() { var viewValue = ctrl.$viewValue; + $timeout.cancel(pendingDebounce); if (ctrl.$$lastCommittedViewValue === viewValue) { return; @@ -2135,21 +2136,12 @@ var requiredDirective = function() { if (!ctrl) return; attr.required = true; // force truthy in case we are on non input element - var validator = function(value) { - if (attr.required && ctrl.$isEmpty(value)) { - ctrl.$setValidity('required', false); - return; - } else { - ctrl.$setValidity('required', true); - return value; - } + ctrl.$validators.required = function(modelValue, viewValue) { + return !attr.required || !ctrl.$isEmpty(viewValue); }; - ctrl.$formatters.push(validator); - ctrl.$parsers.unshift(validator); - attr.$observe('required', function() { - validator(ctrl.$viewValue); + ctrl.$validate(); }); } }; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 545d70a2c7d8..b739b9c19888 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -2510,7 +2510,7 @@ describe('input', function() { compileInput(''); scope.$apply(function() { - scope.name = ''; + scope.name = null; }); expect(inputElm).toBeInvalid();