Skip to content

Instantly share code, notes, and snippets.

@raymyers
Created February 19, 2015 16:15
Show Gist options
  • Save raymyers/c5e4cb1816cd19c3044c to your computer and use it in GitHub Desktop.
Save raymyers/c5e4cb1816cd19c3044c to your computer and use it in GitHub Desktop.
angular directive to validate with a blacklist
angular.module('myapp.directive.blacklist', []).
directive('maBlacklist', [
function() {
return {
restrict: 'A',
require: 'ngModel',
scope: {'blacklist':'=gsBlacklist'},
link: function($scope, $elem, $attrs, modelCtrl) {
var check = function(value) {
var blacklist = $scope.blacklist;
return !_.isArray(blacklist) || blacklist.indexOf(value) === -1;
}
//For DOM -> model validation
modelCtrl.$parsers.unshift(function(value) {
// console.log("parsing", value);
var valid = check(value);
modelCtrl.$setValidity('blacklist', valid);
//return valid ? value : undefined;
return value;
});
//For model -> DOM validation
modelCtrl.$formatters.unshift(function(value) {
modelCtrl.$setValidity('blacklist', check(value));
return value;
});
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment