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

Commit

Permalink
fix(ngRequired): set error correctly when inside ngRepeat and false b…
Browse files Browse the repository at this point in the history
…y default

Previously, in the required validator, we would read the required setting directly
from attr.required, where it is set by ngRequired.

However, when the control is inside ngRepeat, ngRequired sets it only after a another digest has
passed, which means the initial validation run of ngModel does not include the correct required
setting. (Before commit 0637a21 this would not have been a problem,
as every observed value change triggered a validation).

We now use the initially parsed value from ngRequired in the validator.

Fixes #16814
Closes #16820
  • Loading branch information
Narretz committed Jan 26, 2019
1 parent 9f7144b commit 005dd97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ng/directive/validators.js
Expand Up @@ -68,17 +68,17 @@ var requiredDirective = ['$parse', function($parse) {
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
var oldVal = attr.required || $parse(attr.ngRequired)(scope);
var value = attr.required || $parse(attr.ngRequired)(scope);

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

ctrl.$validators.required = function(modelValue, viewValue) {
return !attr.required || !ctrl.$isEmpty(viewValue);
return !value || !ctrl.$isEmpty(viewValue);
};

attr.$observe('required', function(val) {
if (oldVal !== val) {
oldVal = val;
attr.$observe('required', function(newVal) {
if (value !== newVal) {
value = newVal;
ctrl.$validate();
}
});
Expand Down
14 changes: 14 additions & 0 deletions test/ng/directive/validatorsSpec.js
Expand Up @@ -730,5 +730,19 @@ describe('validators', function() {

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

it('should validate once when inside ngRepeat, and set the "required" error when ngRequired is false by default', function() {
$rootScope.isRequired = false;
$rootScope.refs = {};

var elm = helper.compileInput(
'<div ng-repeat="input in [0]">' +
'<input type="text" ng-ref="refs.input" ng-ref-read="ngModel" ng-model="value" ng-required="isRequired" validation-spy="required" />' +
'</div>');

expect(helper.validationCounter.required).toBe(1);
expect($rootScope.refs.input.$error.required).toBeUndefined();
});

});
});

0 comments on commit 005dd97

Please sign in to comment.