Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save replete/7eb8544f00c9c002b9ad9c3f6a19ccc5 to your computer and use it in GitHub Desktop.
Save replete/7eb8544f00c9c002b9ad9c3f6a19ccc5 to your computer and use it in GitHub Desktop.
Angular 1.x blacklisted values validator. Accepts a list of values that are not allowed.
(function() {
'use strict';
angular.module('shopperTrak.validators')
.directive('blacklistedValues', function (){
return {
require: 'ngModel',
restrict:'A',
link: function(scope, elem, attr, ngModel) {
var blacklist = [];
scope.$watch(attr.blacklistedValues, function (values) {
blacklist = values;
});
//For DOM -> model validation
ngModel.$parsers.unshift(function(value) {
var valid = blacklist.indexOf(value) === -1;
ngModel.$setValidity('blacklistedValues', valid);
return valid ? value : undefined;
});
//For model -> DOM validation
ngModel.$formatters.unshift(function(value) {
ngModel.$setValidity('blacklistedValues', blacklist.indexOf(value) === -1);
return value;
});
}
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment