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

Commit a4c7bdc

Browse files
committed
fix(required): correctly validate required on non-input element surrounded by ngIf
Closes #16830 Closes #16836
1 parent edb3e22 commit a4c7bdc

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/ng/directive/validators.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,21 @@ var requiredDirective = ['$parse', function($parse) {
6868
require: '?ngModel',
6969
link: function(scope, elm, attr, ctrl) {
7070
if (!ctrl) return;
71-
var value = attr.required || $parse(attr.ngRequired)(scope);
71+
// For boolean attributes like required, presence means true
72+
var value = attr.hasOwnProperty('required') || $parse(attr.ngRequired)(scope);
7273

73-
attr.required = true; // force truthy in case we are on non input element
74+
if (!attr.ngRequired) {
75+
// force truthy in case we are on non input element
76+
// (input elements do this automatically for boolean attributes like required)
77+
attr.required = true;
78+
}
7479

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

7984
attr.$observe('required', function(newVal) {
85+
8086
if (value !== newVal) {
8187
value = newVal;
8288
ctrl.$validate();

test/ng/directive/validatorsSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,13 @@ describe('validators', function() {
696696
}));
697697

698698

699+
it('should override "required" when ng-required="false" is set', function() {
700+
var inputElm = helper.compileInput('<input type="text" ng-model="notDefined" required ng-required="false" />');
701+
702+
expect(inputElm).toBeValid();
703+
});
704+
705+
699706
it('should validate only once after compilation when inside ngRepeat', function() {
700707
helper.compileInput(
701708
'<div ng-repeat="input in [0]">' +
@@ -731,6 +738,7 @@ describe('validators', function() {
731738
expect(helper.validationCounter.required).toBe(1);
732739
});
733740

741+
734742
it('should validate once when inside ngRepeat, and set the "required" error when ngRequired is false by default', function() {
735743
$rootScope.isRequired = false;
736744
$rootScope.refs = {};
@@ -744,5 +752,15 @@ describe('validators', function() {
744752
expect($rootScope.refs.input.$error.required).toBeUndefined();
745753
});
746754

755+
756+
it('should validate only once when inside ngIf with required on non-input elements', inject(function($compile) {
757+
$rootScope.value = '12';
758+
$rootScope.refs = {};
759+
helper.compileInput('<div ng-if="true"><span ng-model="value" ng-ref="refs.ctrl" ng-ref-read="ngModel" required validation-spy="required"></span></div>');
760+
$rootScope.$digest();
761+
762+
expect(helper.validationCounter.required).toBe(1);
763+
expect($rootScope.refs.ctrl.$error.required).not.toBe(true);
764+
}));
747765
});
748766
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy