This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -68,15 +68,21 @@ var requiredDirective = ['$parse', function($parse) {
68
68
require : '?ngModel' ,
69
69
link : function ( scope , elm , attr , ctrl ) {
70
70
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 ) ;
72
73
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
+ }
74
79
75
80
ctrl . $validators . required = function ( modelValue , viewValue ) {
76
81
return ! value || ! ctrl . $isEmpty ( viewValue ) ;
77
82
} ;
78
83
79
84
attr . $observe ( 'required' , function ( newVal ) {
85
+
80
86
if ( value !== newVal ) {
81
87
value = newVal ;
82
88
ctrl . $validate ( ) ;
Original file line number Diff line number Diff line change @@ -696,6 +696,13 @@ describe('validators', function() {
696
696
} ) ) ;
697
697
698
698
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
+
699
706
it ( 'should validate only once after compilation when inside ngRepeat' , function ( ) {
700
707
helper . compileInput (
701
708
'<div ng-repeat="input in [0]">' +
@@ -731,6 +738,7 @@ describe('validators', function() {
731
738
expect ( helper . validationCounter . required ) . toBe ( 1 ) ;
732
739
} ) ;
733
740
741
+
734
742
it ( 'should validate once when inside ngRepeat, and set the "required" error when ngRequired is false by default' , function ( ) {
735
743
$rootScope . isRequired = false ;
736
744
$rootScope . refs = { } ;
@@ -744,5 +752,15 @@ describe('validators', function() {
744
752
expect ( $rootScope . refs . input . $error . required ) . toBeUndefined ( ) ;
745
753
} ) ;
746
754
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
+ } ) ) ;
747
765
} ) ;
748
766
} ) ;
You can’t perform that action at this time.
0 commit comments