@@ -387,6 +387,7 @@ angular.module('validation.directive', ['validation.provider']);
387387 var $timeout = $injector . get ( '$timeout' ) ;
388388 var $compile = $injector . get ( '$compile' ) ;
389389 var $parse = $injector . get ( '$parse' ) ;
390+ var groups = { } ;
390391
391392 /**
392393 * Do this function if validation valid
@@ -467,29 +468,35 @@ angular.module('validation.directive', ['validation.provider']);
467468 * Verify whether there is one of the elements inside the group valid.
468469 * If so, it returns true, otherwise, it returns false
469470 *
470- * @param scope
471- * @param element
472- * @param attrs
473- * @param ctrl
471+ * @param validationGroup
474472 * @return {boolean }
475473 */
476- var checkValidationGroup = function ( scope , element , attrs , ctrl ) {
477- var validationGroup = attrs . validationGroup ;
478- var validationGroupElems = document . querySelectorAll ( '*[validation-group=' + validationGroup + ']' ) ;
479- var validationGroupElem ;
474+ var checkValidationGroup = function ( validationGroup ) {
475+ var group = groups [ validationGroup ] ;
480476
481- // Set the element to be invalid
482- ctrl . $setValidity ( ctrl . $name , false ) ;
477+ return Object . keys ( group ) . some ( function ( key ) {
478+ return group [ key ] ;
479+ } ) ;
480+ } ;
481+
482+ /**
483+ * Set validity to all elements inside the given group
484+ *
485+ * @param scope
486+ * @param groupName
487+ * @param validity
488+ */
489+ function setValidationGroup ( scope , validationGroup , validity ) {
490+ var validationGroupElems = document . querySelectorAll ( '*[validation-group=' + validationGroup + ']' ) ;
483491
484492 // Loop through all elements inside the group
485493 for ( var i = 0 , len = validationGroupElems . length ; i < len ; i ++ ) {
486- validationGroupElem = angular . element ( validationGroupElems [ i ] ) ;
487-
488- // If the element is valid and it's not the same element with the current checking element, returns true
489- if ( validationGroupElem . hasClass ( 'ng-valid' ) && validationGroupElem [ 0 ] !== element [ 0 ] ) return true ;
494+ var elem = validationGroupElems [ i ] ;
495+ var formName = elem . form . name ;
496+ var elemName = elem . name ;
497+ scope [ formName ] [ elemName ] . $setValidity ( elemName , validity ) ;
490498 }
491- return false ;
492- } ;
499+ }
493500
494501 /**
495502 * collect elements for focus
@@ -543,11 +550,23 @@ angular.module('validation.directive', ['validation.provider']);
543550 if ( expression . constructor === Function ) {
544551 return $q . all ( [ $validationProvider . getExpression ( validator ) ( value , scope , element , attrs , validatorParam ) ] )
545552 . then ( function ( data ) {
546- if ( data && data . length > 0 && data [ 0 ] ) return valid . success ( ) ;
547- else if ( validationGroup ) {
553+ if ( data && data . length > 0 && data [ 0 ] ) {
554+ if ( validationGroup ) {
555+ groups [ validationGroup ] [ ctrl . $name ] = true ;
556+ setValidationGroup ( scope , validationGroup , true ) ;
557+ }
558+ return valid . success ( ) ;
559+ } else if ( validationGroup ) {
560+ groups [ validationGroup ] [ ctrl . $name ] = false ;
561+
548562 // Whenever the element is invalid, we'll check whether one of the elements inside the its group valid or not.
549563 // If there is a valid element, its invalid message won't be shown, Otherwise, shows its invalid message.
550- if ( ! checkValidationGroup ( scope , element , attrs , ctrl ) ) valid . error ( ) ;
564+ if ( checkValidationGroup ( validationGroup ) ) {
565+ setValidationGroup ( scope , validationGroup , true ) ;
566+ } else {
567+ setValidationGroup ( scope , validationGroup , false ) ;
568+ return valid . error ( ) ;
569+ }
551570 } else return valid . error ( ) ;
552571 } , function ( ) {
553572 return valid . error ( ) ;
@@ -557,12 +576,26 @@ angular.module('validation.directive', ['validation.provider']);
557576 // Check with RegExp
558577 else if ( expression . constructor === RegExp ) {
559578 // Only apply the test if the value is neither undefined or null
560- if ( value !== undefined && value !== null ) return $validationProvider . getExpression ( validator ) . test ( value ) ? valid . success ( ) : valid . error ( ) ;
561- else if ( validationGroup ) {
562- // Whenever the element is invalid, we'll check whether one of the elements inside the its group valid or not.
563- // If there is a valid element, its invalid message won't be shown, Otherwise, shows its invalid message.
564- if ( ! checkValidationGroup ( scope , element , attrs , ctrl ) ) valid . error ( ) ;
565- } else return valid . error ( ) ;
579+ if ( value !== undefined && value !== null ) {
580+ if ( $validationProvider . getExpression ( validator ) . test ( value ) ) {
581+ if ( validationGroup ) {
582+ groups [ validationGroup ] [ ctrl . $name ] = true ;
583+ setValidationGroup ( scope , validationGroup , true ) ;
584+ }
585+ return valid . success ( ) ;
586+ } else if ( validationGroup ) {
587+ groups [ validationGroup ] [ ctrl . $name ] = false ;
588+
589+ // Whenever the element is invalid, we'll check whether one of the elements inside the its group valid or not.
590+ // If there is a valid element, its invalid message won't be shown, Otherwise, shows its invalid message.
591+ if ( checkValidationGroup ( validationGroup ) ) {
592+ setValidationGroup ( scope , validationGroup , true ) ;
593+ } else {
594+ setValidationGroup ( scope , validationGroup , false ) ;
595+ return valid . error ( ) ;
596+ }
597+ } else return valid . error ( ) ;
598+ }
566599 } else return valid . error ( ) ;
567600 } ;
568601
@@ -622,6 +655,14 @@ angular.module('validation.directive', ['validation.provider']);
622655 initialValidity = scope . initialValidity ;
623656 }
624657
658+ /**
659+ * Set up groups object in order to keep track validation of elements
660+ */
661+ if ( validationGroup ) {
662+ if ( ! groups [ validationGroup ] ) groups [ validationGroup ] = { } ;
663+ groups [ validationGroup ] [ ctrl . $name ] = false ;
664+ }
665+
625666 /**
626667 * Default Valid/Invalid Message
627668 */
0 commit comments