This repository was archived by the owner on Apr 12, 2024. It is now read-only.
  
  
  
  
    
    
    
      
    
  
  
    
File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 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) {
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 ( ) ; 
Original file line number Diff line number Diff 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} ) ; 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments