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

WIP fix(ngRequired): always evaluate ngRequired at least once #16815

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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