Skip to content

Commit

Permalink
Merge pull request #32 from sagrawal31/error-msg-fixes
Browse files Browse the repository at this point in the history
Error message fixes & improvements
  • Loading branch information
sagrawal31 authored Apr 20, 2017
2 parents db90438 + eee3024 commit e1814ae
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/bootstrap-angular-validation-all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bootstrap-angular-validation-core.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bootstrap-angular-validation-simple.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/app.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
this.global.errorMessagePrefix = '';
this.global.tooltipPlacement = 'bottom-left';
this.global.tooltipAppendToBody = false;
this.global.suppressWarnings = false;

this.global.messages = {
required: 'This field is required.',
Expand Down Expand Up @@ -86,6 +87,7 @@ angular.module('bootstrap.angular.validation').provider('bsValidationConfig', fu
messages: _this.global.messages,
errorClass: _this.global.errorClass,
successClass: _this.global.successClass,
suppressWarnings: _this.global.suppressWarnings,
tooltipAppendToBody: _this.global.tooltipAppendToBody,

getDisplayErrorsAs: function () {
Expand Down
13 changes: 10 additions & 3 deletions src/directives/validation.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* @requires BsValidationService
*/
angular.module('bootstrap.angular.validation').directive('bsValidation', [
'$timeout', '$injector', 'BsValidationService', function ($timeout, $injector, validationService) {
'$timeout', '$injector', 'BsValidationService', 'bsValidationConfig',
function ($timeout, $injector, validationService, bsValidationConfig) {
return {
restrict: 'A',
require: ['?ngModel', '?^^form'],
Expand All @@ -22,12 +23,18 @@ angular.module('bootstrap.angular.validation').directive('bsValidation', [
var ngFormController = controllers[1];

if (!ngModelController) {
throw 'ng-model directive is required for the bs-validation directive to work.';
if (!bsValidationConfig.suppressWarnings) {
console.warn('ng-model directive is required for the bs-validation directive to work.');
}
return;
}

var $formGroupElement = validationService.getFormGroupElement($element);
if (!$formGroupElement) {
throw 'No parent form group element found for input element';
if (!bsValidationConfig.suppressWarnings) {
console.warn('No parent form group element found for input element');
}
return;
}

var displayValidationState = false;
Expand Down
36 changes: 28 additions & 8 deletions src/services/simple.message.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ angular.module('bootstrap.angular.validation').factory('simpleMessageService', [
var errorElementClass = '.bs-invalid-msg';

function getErrorContainer($element, $formGroupElement) {
var $errorElement = $formGroupElement.find(errorElementClass);
if ($errorElement && $errorElement.length) {
return $errorElement;
var $errorContainer;

// If input element has "id" attribute
if ($element.attr('id')) {
// Then first try to find the error container with the same id prefixed with "bs-error-"
$errorContainer = $formGroupElement.find('#bs-error-' + $element.attr('id'));
if ($errorContainer && $errorContainer.length) {
return $errorContainer;
}
}

$errorContainer = $formGroupElement.find(errorElementClass);
if ($errorContainer && $errorContainer.length) {
return $errorContainer;
}

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

insertAfter.after('<span class="help-block ' + errorElementClass.substring(1) + '"></span>');
return $formGroupElement.find(errorElementClass);
var errorContainerHTML = '<span class="help-block ' + errorElementClass.substring(1) + '" ';
if ($element.attr('id')) {
errorContainerHTML += 'id="bs-error-' + $element.attr('id') + '"';
}
errorContainerHTML += '></span>';
$errorContainer = angular.element(errorContainerHTML);

insertAfter.after($errorContainer);
return $errorContainer;
}

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

hideErrorMessage: function ($element, $formGroupElement) {
validationService.removeErrorClass($formGroupElement);
$formGroupElement.find(errorElementClass).addClass('ng-hide');

var $errorContainer = getErrorContainer($element, $formGroupElement);
$errorContainer.html('').addClass('ng-hide');
},

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

var $errorElement = getErrorContainer($element, $formGroupElement);
$errorElement.html(message).removeClass('ng-hide');
var $errorContainer = getErrorContainer($element, $formGroupElement);
$errorContainer.html(message).removeClass('ng-hide');
}
};
}]);
8 changes: 4 additions & 4 deletions src/services/validation.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ angular.module('bootstrap.angular.validation').factory('BsValidationService', ['
},

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

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

if (!formGroupElement || formGroupElement.length === 0) {
return null;
Expand Down

0 comments on commit e1814ae

Please sign in to comment.