Created
April 18, 2017 16:02
-
-
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.
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
(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