I'm trying to build a directive for input fields which validates using a custom function passed from the outer scope, for example:
HTML:
<input type="text" custom-validator="checkValidity"/>
Controller:
$scope.checkValidity = function(value){
return $scope.invalidWords.indexOf(value) === -1;
}
I've created a detailed plunker for this: http://plnkr.co/edit/H5A5O3?p=preview
Validation works, but it seems to broke things up with the default directives, in this case ng-disabled doesn't work and I can't access the variable used in ng-model!
This is my directive:
app.directive('customValidator', function() {
return {
require: "ngModel"
, scope: { customValidator: '='}
, link: function postLink(scope, elm, attrs, ctrl) {
var validator = function(value) {
if(scope.customValidator && scope.customValidator(value)) {
ctrl.$setValidity('custom-validator', true);
return value;
}
ctrl.$setValidity('custom-validator', false);
return undefined;
}
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
}
});
I can't figure out what's going wrong, I really need help!
I should stay on Angular 1.0.7