1313 * parent element with class '.form-group' and will show/hide the validation message automatically.
1414*/
1515angular . module ( 'bootstrap.angular.validation' ) . directive ( 'bsValidation' , [
16- '$interpolate' , '$timeout' , 'BsValidationService' , 'bsValidationConfig' ,
16+ '$interpolate' , '$timeout' , '$injector' , ' BsValidationService', 'bsValidationConfig' ,
1717
18- function ( $interpolate , $timeout , bsValidationService , bsValidationConfig ) {
18+ function ( $interpolate , $timeout , $injector , bsValidationService , bsValidationConfig ) {
1919 return {
2020 restrict : 'A' ,
2121 require : [ 'ngModel' , '?^^form' ] ,
@@ -26,38 +26,25 @@ angular.module('bootstrap.angular.validation').directive('bsValidation', [
2626
2727 // HTML selector helper
2828 var errorElementClass = 'bs-invalid-msg' ;
29- var iconMarkup = '<i class="fa fa-exclamation-triangle fa-fw"></i>' ;
30- var formGroup = '.form-group' ;
31- var customFormGroup = '[bs-form-group]' ;
29+
3230 var helpBlock = 'help-block' ;
3331
3432 // All classed needed to add to validation message
3533 var errorClasses = [ errorElementClass , helpBlock ] ;
36- var markupClasses = errorClasses . join ( ' ' ) ;
37-
38- // Search parent element with class form-group to operate on.
39- var formGroupElement = $element . parents ( formGroup ) ;
40-
41- // Search for an attribute 'bs-form-group' if the class '.form-group' is not available
42- if ( ! formGroupElement || formGroupElement . length === 0 ) {
43- formGroupElement = $element . parents ( customFormGroup ) ;
4434
45- // If we still don't find any element
46- if ( ! formGroupElement || formGroupElement . length === 0 ) {
47- // Then do not execute the directive at all
48- return ;
49- }
35+ var $formGroupElement = bsValidationService . getFormGroupElement ( $element ) ;
36+ if ( ! $formGroupElement ) {
37+ throw 'No parent form group element found for input element' ;
5038 }
5139
5240 var displayValidationState = false ;
5341 var shouldValidateOnBlur = bsValidationConfig . shouldValidateOnBlur ( ) ;
5442 var shouldValidateOnDisplay = bsValidationConfig . shouldValidateOnDisplay ( ) ;
5543 var shouldValidateOnSubmit = bsValidationConfig . shouldValidateOnSubmit ( ) ;
5644
57- var metaInformation = { } ;
58- angular . forEach ( bsValidationService . getMeta ( ) , function ( key ) {
59- metaInformation [ key ] = $element . attr ( key ) ;
60- } ) ;
45+ var metaInformation = bsValidationService . getMetaInformation ( $element ) ;
46+ var displayErrorAs = bsValidationService . displayErrorPreference ( $element , $attr ) ;
47+ var validationMessageService = bsValidationService . getValidationMessageService ( displayErrorAs ) ;
6148
6249 // Register generic custom validators if added to element
6350 angular . forEach ( bsValidationService . getValidators ( ) , function ( key ) {
@@ -67,63 +54,29 @@ angular.module('bootstrap.angular.validation').directive('bsValidation', [
6754 }
6855 } ) ;
6956
70- function resolveMessage ( key ) {
71- var message = $element . attr ( key + '-notification' ) || bsValidationService . getDefaultMessage ( key ) ;
72- var matchers = angular . extend ( { } , { validValue : $attr [ key ] } , metaInformation ) ;
73- return $interpolate ( message ) ( matchers ) ;
74- }
75-
7657 function removeErrors ( ) {
77- formGroupElement . removeClass ( 'has-error' ) ;
78- if ( formGroupElement . length > 0 ) { formGroupElement . findAll ( 'span.' + errorClasses . join ( '.' ) ) . addClass ( 'ng-hide' ) ; }
79- return false ;
58+ validationMessageService . hideErrorMessage ( $formGroupElement ) ;
8059 }
8160
8261 function removeSuccessClass ( ) {
83- formGroupElement . removeClass ( 'has-success' ) ;
62+ $ formGroupElement. removeClass ( 'has-success' ) ;
8463 }
8564
8665 function addErrors ( ) {
87- addErrorClass ( ) ;
88- addErrorMessage ( ) ;
66+ bsValidationService . addErrorClass ( $formGroupElement ) ;
67+ validationMessageService . showErrorMessage ( $element , $attr , ngModelController , $formGroupElement ) ;
8968 return false ;
9069 }
9170
9271 function addSuccessClass ( ) {
93- formGroupElement . addClass ( 'has-success' ) ;
72+ $ formGroupElement. addClass ( 'has-success' ) ;
9473 return removeErrors ( ) ;
9574 }
9675
97- function addErrorClass ( ) {
98- formGroupElement . removeClass ( 'has-success' ) ;
99- formGroupElement . addClass ( 'has-error' ) ;
100- }
101-
102- function renderError ( message ) {
103- return '<span class="' + markupClasses + '">' + iconMarkup + message + '</span>' ;
104- }
105-
106- function errorContainer ( message ) {
107- var insertAfter = $element ;
108- // Check if the container have any Bootstrap input group then append the error after it
109- var groupElement = formGroupElement . findOne ( '.input-group' ) ;
110- if ( groupElement . length > 0 ) {
111- insertAfter = groupElement ;
112- }
113- insertAfter . after ( renderError ( message ) ) ;
114- }
115-
116- function addErrorMessage ( ) {
117- var message = resolveMessage ( Object . keys ( ngModelController . $error ) [ 0 ] ) ;
118- var errorElement = formGroupElement . findOne ( '.' + errorElementClass ) ;
119- if ( errorElement . length === 0 ) { errorContainer ( message ) ; }
120- errorElement . html ( iconMarkup + message ) . removeClass ( 'ng-hide' ) ;
121- }
122-
12376 function displayOrHideValidationState ( ) {
12477 if ( ! displayValidationState ) {
12578 removeSuccessClass ( ) ;
126- return removeErrors ( ) ;
79+ removeErrors ( ) ;
12780 }
12881
12982 if ( ngModelController . $valid ) { return addSuccessClass ( ) ; }
0 commit comments