Skip to content

Commit 5d952e3

Browse files
Merge pull request #134 from angular/master
Support multiple versions of
2 parents 087f765 + 005dd97 commit 5d952e3

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ng/directive/validators.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ var requiredDirective = ['$parse', function($parse) {
6868
require: '?ngModel',
6969
link: function(scope, elm, attr, ctrl) {
7070
if (!ctrl) return;
71-
var oldVal = attr.required || $parse(attr.ngRequired)(scope);
71+
var value = attr.required || $parse(attr.ngRequired)(scope);
7272

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

7575
ctrl.$validators.required = function(modelValue, viewValue) {
76-
return !attr.required || !ctrl.$isEmpty(viewValue);
76+
return !value || !ctrl.$isEmpty(viewValue);
7777
};
7878

79-
attr.$observe('required', function(val) {
80-
if (oldVal !== val) {
81-
oldVal = val;
79+
attr.$observe('required', function(newVal) {
80+
if (value !== newVal) {
81+
value = newVal;
8282
ctrl.$validate();
8383
}
8484
});

test/ng/directive/validatorsSpec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,5 +730,19 @@ describe('validators', function() {
730730

731731
expect(helper.validationCounter.required).toBe(1);
732732
});
733+
734+
it('should validate once when inside ngRepeat, and set the "required" error when ngRequired is false by default', function() {
735+
$rootScope.isRequired = false;
736+
$rootScope.refs = {};
737+
738+
var elm = helper.compileInput(
739+
'<div ng-repeat="input in [0]">' +
740+
'<input type="text" ng-ref="refs.input" ng-ref-read="ngModel" ng-model="value" ng-required="isRequired" validation-spy="required" />' +
741+
'</div>');
742+
743+
expect(helper.validationCounter.required).toBe(1);
744+
expect($rootScope.refs.input.$error.required).toBeUndefined();
745+
});
746+
733747
});
734748
});

0 commit comments

Comments
 (0)