Skip to content

Commit 85f291c

Browse files
committed
Displaying error message as tooltip functional. #9
1 parent 40245f7 commit 85f291c

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

dist/bootstrap-angular-validation.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/directives/validation.directive.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ angular.module('bootstrap.angular.validation').directive('bsValidation', [
2424
var ngModelController = controllers[0];
2525
var ngFormController = controllers[1];
2626

27-
// HTML selector helper
28-
var errorElementClass = 'bs-invalid-msg';
29-
30-
var helpBlock = 'help-block';
31-
32-
// All classed needed to add to validation message
33-
var errorClasses = [errorElementClass, helpBlock];
34-
3527
var $formGroupElement = validationService.getFormGroupElement($element);
3628
if (!$formGroupElement) {
3729
throw 'No parent form group element found for input element';
@@ -55,7 +47,7 @@ angular.module('bootstrap.angular.validation').directive('bsValidation', [
5547
});
5648

5749
function removeErrors() {
58-
validationMessageService.hideErrorMessage($formGroupElement);
50+
validationMessageService.hideErrorMessage($element, $formGroupElement);
5951
}
6052

6153
function removeSuccessClass() {

src/services/simple.message.service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
angular.module('bootstrap.angular.validation').factory('simpleMessageService', ['BsValidationService', function(bsValidationService) {
3+
angular.module('bootstrap.angular.validation').factory('simpleMessageService', ['BsValidationService', function(validationService) {
44

55
var errorElementClass = '.bs-invalid-msg';
66
var helpBlockClass = '.help-block';
@@ -27,13 +27,13 @@ angular.module('bootstrap.angular.validation').factory('simpleMessageService', [
2727
}
2828

2929
return {
30-
hideErrorMessage: function($formGroupElement) {
31-
$formGroupElement.removeClass('has-error');
30+
hideErrorMessage: function($element, $formGroupElement) {
31+
validationService.removeErrorClass($formGroupElement);
3232
$formGroupElement.findAll('span.' + errorClasses.join('.')).addClass('ng-hide');
3333
},
3434

3535
resolveMessage: function($element, $attr, key) {
36-
return bsValidationService.resolveMessage($element, $attr, key);
36+
return validationService.resolveMessage($element, $attr, key);
3737
},
3838

3939
showErrorMessage: function($element, $attr, ngModelController, $formGroupElement) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* global document */
2+
3+
'use strict';
4+
5+
angular.module('bootstrap.angular.validation').factory('tooltipMessageService', ['$uibPosition', 'BsValidationService',
6+
function($uibPosition, validationService) {
7+
8+
var iconMarkup = '<i class="fa fa-exclamation-triangle fa-fw"></i>';
9+
10+
function getElementID($element) {
11+
var id = $element.attr('id');
12+
13+
if (id) {
14+
return id;
15+
}
16+
17+
id = 'bs-' + (Math.floor(Math.random() * 10000)) + '-' + Math.floor(Math.random() * 10000);
18+
$element.attr('id', id);
19+
return id;
20+
}
21+
22+
function getErrorTooltipID($element) {
23+
return 'bs-error-' + getElementID($element);
24+
}
25+
26+
function getErrorTooltip($element) {
27+
var tooltipID = getErrorTooltipID($element);
28+
return angular.element(document.getElementById(tooltipID));
29+
}
30+
31+
return {
32+
hideErrorMessage: function($element, $formGroupElement) {
33+
validationService.removeErrorClass($formGroupElement);
34+
getErrorTooltip($element).removeClass('in');
35+
},
36+
37+
resolveMessage: function($element, $attr, key) {
38+
return validationService.resolveMessage($element, $attr, key);
39+
},
40+
41+
showErrorMessage: function($element, $attr, ngModelController, $formGroupElement) {
42+
var firstErrorKey = Object.keys(ngModelController.$error)[0];
43+
var message = this.resolveMessage($element, $attr, firstErrorKey);
44+
45+
var tooltipID = getErrorTooltipID($element);
46+
var $errorTooltip = getErrorTooltip($element);
47+
48+
if (!$errorTooltip || !$errorTooltip.length) {
49+
angular.element('body').append('<div class="tooltip has-error" id="' + tooltipID + '" role="tooltip"' +
50+
'><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>');
51+
52+
$errorTooltip = getErrorTooltip($element);
53+
}
54+
55+
var ttPosition = $uibPosition.positionElements($element, $errorTooltip, 'bottom-left', true);
56+
$errorTooltip.css({ top: ttPosition.top + 'px', left: ttPosition.left + 'px' });
57+
$errorTooltip.findOne('.tooltip-inner').html(message);
58+
$errorTooltip.addClass('in').addClass(ttPosition.placement);
59+
}
60+
};
61+
}]);

0 commit comments

Comments
 (0)