Created
February 19, 2015 16:15
-
-
Save raymyers/c5e4cb1816cd19c3044c to your computer and use it in GitHub Desktop.
angular directive to validate with a blacklist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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