Skip to content

Commit

Permalink
fix(ngRequired): always evaluate ngRequired at least once
Browse files Browse the repository at this point in the history
by default ngRequired is set to true, but the original value can be
false and never be updated.

Fixes: angular#16814
  • Loading branch information
Cody Mikol committed Jan 18, 2019
1 parent c7671ed commit 0693a37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ng/directive/validators.js
Expand Up @@ -69,6 +69,7 @@ var requiredDirective = ['$parse', function($parse) {
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
var oldVal = attr.required || $parse(attr.ngRequired)(scope);
var evaluated = false;

attr.required = true; // force truthy in case we are on non input element

Expand All @@ -77,9 +78,10 @@ var requiredDirective = ['$parse', function($parse) {
};

attr.$observe('required', function(val) {
if (oldVal !== val) {
if (oldVal !== val || !evaluated) {
oldVal = val;
ctrl.$validate();
evaluated = true;
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions test/ng/directive/validatorsSpec.js
Expand Up @@ -707,6 +707,15 @@ describe('validators', function() {
expect(helper.validationCounter.required).toBe(1);
});

it('should validate once when inside ngRepeat and ngRequired is false as ngRequired sets a default required flag of true', function() {
$rootScope.isRequired = false;
helper.compileInput(
'<div ng-repeat="input in [0]">' +
'<input type="text" ng-model="value" ng-required="isRequired" validation-spy="required" />' +
'</div>');

expect(helper.validationCounter.required).toBe(1);
});

it('should validate only once after compilation when inside ngRepeat and ngRequired is true', function() {
$rootScope.isRequired = true;
Expand Down

0 comments on commit 0693a37

Please sign in to comment.