Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple versions of #134

Merged
merged 1 commit into from Jan 30, 2019
Merged
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
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();
});

});
});