This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +19
-2
lines changed
2 files changed +19
-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 = 'required' in attr || $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 @@ -731,6 +731,7 @@ describe('validators', function() {
731
731
expect ( helper . validationCounter . required ) . toBe ( 1 ) ;
732
732
} ) ;
733
733
734
+
734
735
it ( 'should validate once when inside ngRepeat, and set the "required" error when ngRequired is false by default' , function ( ) {
735
736
$rootScope . isRequired = false ;
736
737
$rootScope . refs = { } ;
@@ -744,5 +745,15 @@ describe('validators', function() {
744
745
expect ( $rootScope . refs . input . $error . required ) . toBeUndefined ( ) ;
745
746
} ) ;
746
747
748
+
749
+ it ( 'should validate only once when inside ngIf with required on non-input elements' , inject ( function ( $compile ) {
750
+ $rootScope . value = '12' ;
751
+ $rootScope . refs = { } ;
752
+ helper . compileInput ( '<div ng-if="true"><span ng-model="value" ng-ref="refs.ctrl" ng-ref-read="ngModel" required validation-spy="required"></span></div>' ) ;
753
+ $rootScope . $digest ( ) ;
754
+
755
+ expect ( helper . validationCounter . required ) . toBe ( 1 ) ;
756
+ expect ( $rootScope . refs . ctrl . $error . required ) . not . toBe ( true ) ;
757
+ } ) ) ;
747
758
} ) ;
748
759
} ) ;
You can’t perform that action at this time.
0 commit comments