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

fix(required): correctly validate when required on non-input element … #16836

Merged
merged 2 commits into from Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 2 deletions src/ng/directive/validators.js
Expand Up @@ -68,15 +68,21 @@ var requiredDirective = ['$parse', function($parse) {
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
var value = attr.required || $parse(attr.ngRequired)(scope);
// For boolean attributes like required, presence means true
var value = 'required' in attr || $parse(attr.ngRequired)(scope);
petebacondarwin marked this conversation as resolved.
Show resolved Hide resolved

attr.required = true; // force truthy in case we are on non input element
if (!attr.ngRequired) {
// force truthy in case we are on non input element
// (input elements do this automatically for boolean attributes like required)
attr.required = true;
Narretz marked this conversation as resolved.
Show resolved Hide resolved
}

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

attr.$observe('required', function(newVal) {

if (value !== newVal) {
value = newVal;
ctrl.$validate();
Expand Down
11 changes: 11 additions & 0 deletions test/ng/directive/validatorsSpec.js
Expand Up @@ -731,6 +731,7 @@ 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 = {};
Expand All @@ -744,5 +745,15 @@ describe('validators', function() {
expect($rootScope.refs.input.$error.required).toBeUndefined();
});


it('should validate only once when inside ngIf with required on non-input elements', inject(function($compile) {
$rootScope.value = '12';
$rootScope.refs = {};
helper.compileInput('<div ng-if="true"><span ng-model="value" ng-ref="refs.ctrl" ng-ref-read="ngModel" required validation-spy="required"></span></div>');
$rootScope.$digest();

expect(helper.validationCounter.required).toBe(1);
expect($rootScope.refs.ctrl.$error.required).not.toBe(true);
}));
});
});