Skip to content

Commit

Permalink
fix(NgModel): make sure the required validator uses the $validators p…
Browse files Browse the repository at this point in the history
…ipeline

Fixes angular#5164
  • Loading branch information
matsko committed Jun 10, 2014
1 parent 678764c commit 960d32b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
16 changes: 4 additions & 12 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
*/
this.$commitViewValue = function() {
var viewValue = ctrl.$viewValue;

$timeout.cancel(pendingDebounce);
if (ctrl.$$lastCommittedViewValue === viewValue) {
return;
Expand Down Expand Up @@ -2130,21 +2131,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();
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,7 @@ describe('input', function() {
compileInput('<input type="text" ng-model="name" name="alias" required />');

scope.$apply(function() {
scope.name = '';
scope.name = null;
});

expect(inputElm).toBeInvalid();
Expand Down

0 comments on commit 960d32b

Please sign in to comment.