Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var addValidationMessage = function (ngForm, elm, errAttr, errMsg, msgVar1) {
- //Use $timeout to ensure validation rules are added and compiled.
- //After compile is done then will start watching errors
- $timeout(function(){
- var elmModel;
- var ngModelName="";
- //Get the name of the 'ng-model' of the element being validated
- elmModel = angular.element(elm).controller('ngModel');
- if (elmModel && elmModel.$name) {
- ngModelName = elmModel.$name;
- }
- if (!ngModelName) {
- ngModelName = angular.element(elm).attr('ng-model');
- }
- if (ngModelName) {
- scope.$watch(ngForm.$name + '.' + ngModelName + '.$error.' + errAttr,
- function (newValue, oldValue){
- //console.log("elm.id =", elm.id);
- //The validation error message will be placed on the element 'title' attribute which will be the field 'tooltip'.
- //newValue == true means there is error
- if (newValue) {
- var msgVar1Val;
- //Perform variable substitution if required to get the final text of the error message.
- if (msgVar1) {
- msgVar1Val = scope.$eval(angular.element(elm).attr(msgVar1));
- errMsg = errMsg.format(msgVar1Val);
- }
- //Append the error to the title if neeeded
- if (elm.title) {
- elm.title += " ";
- } else {
- elm.title = "";
- }
- elm.title += errMsg;
- } else {
- //Remove the error if valid.
- //child.removeAttribute('title');
- if (elm.title) {
- //Replace the error message with blank.
- elm.title = elm.title.replace(errMsg, "").trim();
- }
- }
- });
- } else {
- //console.warn("Warning in addValidationMessage() for element ID '%s' in ngForm '%s'. Message: 'ng-model' is not defined.", elm.id, ngForm.$name)
- }
- }, 1000);
- }
- errMsg = "Number of characters entered should not exceed '{0}' characters.";
- addValidationMessage(ngForm, child, 'maxlength', errMsg, 'ng-maxlength');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement