Skip to content

Instantly share code, notes, and snippets.

@rhutchison
Created March 20, 2016 21:36
Show Gist options
  • Save rhutchison/c8c14946e88a1c8f9216 to your computer and use it in GitHub Desktop.
Save rhutchison/c8c14946e88a1c8f9216 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
/**
* Edits by Ryan Hutchison
* Credit: https://github.com/paulyoder/angular-bootstrap-show-errors */
angular
.module('core')
.directive('showErrors', showErrors);
showErrors.$inject = ['$timeout', '$interpolate'];
function showErrors($timeout, $interpolate) {
var directive = {
restrict: 'A',
require: '^form',
compile: compile
};
return directive;
function compile(elem, attrs) {
if (attrs.showErrors.indexOf('skipFormGroupCheck') === -1) {
if (!(elem.hasClass('form-group') || elem.hasClass('input-group'))) {
throw new Error('show-errors element does not have the \'form-group\' or \'input-group\' class');
}
}
return linkFn;
function linkFn(scope, el, attrs, formCtrl) {
var inputEl,
inputName,
inputNgEl,
options,
showSuccess,
initCheck = false,
showValidationMessages = false;
options = scope.$eval(attrs.showErrors) || {};
showSuccess = options.showSuccess || false;
inputEl = el[0].querySelector('.form-control[name]') || el[0].querySelector('[name]');
inputNgEl = angular.element(inputEl);
inputName = $interpolate(inputNgEl.attr('name') || '')(scope);
if (!inputName) {
throw new Error('show-errors element has no child input elements with a \'name\' attribute class');
}
scope.$watch(function () {
return formCtrl[inputName] && formCtrl[inputName].$invalid;
}, toggleClasses);
scope.$on('show-errors-check-validity', checkValidity);
scope.$on('show-errors-reset', reset);
function checkValidity(event, name) {
if (angular.isUndefined(name) || formCtrl.$name === name) {
initCheck = true;
showValidationMessages = true;
return toggleClasses(formCtrl[inputName].$invalid);
}
}
function reset(event, name) {
if (angular.isUndefined(name) || formCtrl.$name === name) {
return $timeout(function () {
el.removeClass('has-error');
el.removeClass('has-success');
showValidationMessages = false;
}, 0, false);
}
}
function toggleClasses(invalid) {
el.toggleClass('has-error', showValidationMessages && invalid);
if (showSuccess) {
return el.toggleClass('has-success', showValidationMessages && !invalid);
}
}
}
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment