Skip to content

Instantly share code, notes, and snippets.

@trevorhreed
Created June 23, 2014 16:12
Show Gist options
  • Save trevorhreed/9b285f39b1d2e93d87c3 to your computer and use it in GitHub Desktop.
Save trevorhreed/9b285f39b1d2e93d87c3 to your computer and use it in GitHub Desktop.
define([
], function (
) {
return [function formValidator() {
return {
require: '^form',
scope: {
formData: '=',
validateAll: '='
},
link: function(scope, element, attrs, ctrls) {
window.frm = ctrls;
var selector = '.ng-dirty.ng-invalid';
function validate(){
$(".formValidator-input-validation-error-message").remove();
element.find(selector).each(function(index, el){
$el = $(el);
var messages = [];
var classes = $el.attr('class').match(/[\d\w-_]+/g);
for(var i in classes){
var lastIndex = classes[i].lastIndexOf('-invalid-');
if(lastIndex != -1){
var validationMessageAttr = "data-" + classes[i].substr(lastIndex + 9) + "-validation-message";
var msg = $el.attr(validationMessageAttr);
if(!msg){
msg = element.attr(validationMessageAttr);
if(!msg){
msg = "Invalid!";
}
}
messages.push("<div class='validator'>" + msg + "</div>");
}
}
$(el).after("<div style='position:absolute;' class='formValidator-input-validation-error-message'>" + messages.join() + "</div>");
});
}
scope.$watch(function(){
return scope.formData;
}, function(){
validate();
}, true);
scope.$watch('validateAll', function(newValue, oldValue){
selector = !!newValue ? '.ng-invalid' : '.ng-dirty.ng-invalid';
validate();
});
}
};
}];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment