Skip to content

Commit e1814ae

Browse files
authored
Merge pull request #32 from sagrawal31/error-msg-fixes
Error message fixes & improvements
2 parents db90438 + eee3024 commit e1814ae

File tree

7 files changed

+47
-18
lines changed

7 files changed

+47
-18
lines changed

dist/bootstrap-angular-validation-all.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bootstrap-angular-validation-core.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bootstrap-angular-validation-simple.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.provider.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
2727
this.global.errorMessagePrefix = '';
2828
this.global.tooltipPlacement = 'bottom-left';
2929
this.global.tooltipAppendToBody = false;
30+
this.global.suppressWarnings = false;
3031

3132
this.global.messages = {
3233
required: 'This field is required.',
@@ -86,6 +87,7 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
8687
messages: _this.global.messages,
8788
errorClass: _this.global.errorClass,
8889
successClass: _this.global.successClass,
90+
suppressWarnings: _this.global.suppressWarnings,
8991
tooltipAppendToBody: _this.global.tooltipAppendToBody,
9092

9193
getDisplayErrorsAs: function () {

src/directives/validation.directive.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* @requires BsValidationService
99
*/
1010
angular.module('bootstrap.angular.validation').directive('bsValidation', [
11-
'$timeout', '$injector', 'BsValidationService', function ($timeout, $injector, validationService) {
11+
'$timeout', '$injector', 'BsValidationService', 'bsValidationConfig',
12+
function ($timeout, $injector, validationService, bsValidationConfig) {
1213
return {
1314
restrict: 'A',
1415
require: ['?ngModel', '?^^form'],
@@ -22,12 +23,18 @@ angular.module('bootstrap.angular.validation').directive('bsValidation', [
2223
var ngFormController = controllers[1];
2324

2425
if (!ngModelController) {
25-
throw 'ng-model directive is required for the bs-validation directive to work.';
26+
if (!bsValidationConfig.suppressWarnings) {
27+
console.warn('ng-model directive is required for the bs-validation directive to work.');
28+
}
29+
return;
2630
}
2731

2832
var $formGroupElement = validationService.getFormGroupElement($element);
2933
if (!$formGroupElement) {
30-
throw 'No parent form group element found for input element';
34+
if (!bsValidationConfig.suppressWarnings) {
35+
console.warn('No parent form group element found for input element');
36+
}
37+
return;
3138
}
3239

3340
var displayValidationState = false;

src/services/simple.message.service.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@ angular.module('bootstrap.angular.validation').factory('simpleMessageService', [
55
var errorElementClass = '.bs-invalid-msg';
66

77
function getErrorContainer($element, $formGroupElement) {
8-
var $errorElement = $formGroupElement.find(errorElementClass);
9-
if ($errorElement && $errorElement.length) {
10-
return $errorElement;
8+
var $errorContainer;
9+
10+
// If input element has "id" attribute
11+
if ($element.attr('id')) {
12+
// Then first try to find the error container with the same id prefixed with "bs-error-"
13+
$errorContainer = $formGroupElement.find('#bs-error-' + $element.attr('id'));
14+
if ($errorContainer && $errorContainer.length) {
15+
return $errorContainer;
16+
}
17+
}
18+
19+
$errorContainer = $formGroupElement.find(errorElementClass);
20+
if ($errorContainer && $errorContainer.length) {
21+
return $errorContainer;
1122
}
1223

1324
var insertAfter;
@@ -20,8 +31,15 @@ angular.module('bootstrap.angular.validation').factory('simpleMessageService', [
2031
insertAfter = $element;
2132
}
2233

23-
insertAfter.after('<span class="help-block ' + errorElementClass.substring(1) + '"></span>');
24-
return $formGroupElement.find(errorElementClass);
34+
var errorContainerHTML = '<span class="help-block ' + errorElementClass.substring(1) + '" ';
35+
if ($element.attr('id')) {
36+
errorContainerHTML += 'id="bs-error-' + $element.attr('id') + '"';
37+
}
38+
errorContainerHTML += '></span>';
39+
$errorContainer = angular.element(errorContainerHTML);
40+
41+
insertAfter.after($errorContainer);
42+
return $errorContainer;
2543
}
2644

2745
return {
@@ -31,14 +49,16 @@ angular.module('bootstrap.angular.validation').factory('simpleMessageService', [
3149

3250
hideErrorMessage: function ($element, $formGroupElement) {
3351
validationService.removeErrorClass($formGroupElement);
34-
$formGroupElement.find(errorElementClass).addClass('ng-hide');
52+
53+
var $errorContainer = getErrorContainer($element, $formGroupElement);
54+
$errorContainer.html('').addClass('ng-hide');
3555
},
3656

3757
showErrorMessage: function ($element, $attr, ngModelController, $formGroupElement) {
3858
var message = validationService.getErrorMessage($element, $attr, ngModelController);
3959

40-
var $errorElement = getErrorContainer($element, $formGroupElement);
41-
$errorElement.html(message).removeClass('ng-hide');
60+
var $errorContainer = getErrorContainer($element, $formGroupElement);
61+
$errorContainer.html(message).removeClass('ng-hide');
4262
}
4363
};
4464
}]);

src/services/validation.service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ angular.module('bootstrap.angular.validation').factory('BsValidationService', ['
151151
},
152152

153153
getFormGroupElement: function ($element) {
154-
// Search parent element with class form-group to operate on.
155-
var formGroupElement = $element.parents(formGroupClass);
154+
// First search for an attribute with 'bs-form-group'
155+
var formGroupElement = $element.parents(customFormGroup);
156156

157-
// Search for an attribute 'bs-form-group' if the class '.form-group' is not available
158157
if (!formGroupElement || formGroupElement.length === 0) {
159-
formGroupElement = $element.parents(customFormGroup);
158+
// Then search for parent element with class form-group
159+
formGroupElement = $element.parents(formGroupClass);
160160

161161
if (!formGroupElement || formGroupElement.length === 0) {
162162
return null;

0 commit comments

Comments
 (0)