Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. var addValidationMessage = function (ngForm, elm, errAttr, errMsg, msgVar1) {
  2. //Use $timeout to ensure validation rules are added and compiled.
  3. //After compile is done then will start watching errors
  4. $timeout(function(){
  5. var elmModel;
  6. var ngModelName="";
  7. //Get the name of the 'ng-model' of the element being validated
  8. elmModel = angular.element(elm).controller('ngModel');
  9. if (elmModel && elmModel.$name) {
  10. ngModelName = elmModel.$name;
  11. }
  12. if (!ngModelName) {
  13. ngModelName = angular.element(elm).attr('ng-model');
  14. }
  15. if (ngModelName) {
  16. scope.$watch(ngForm.$name + '.' + ngModelName + '.$error.' + errAttr,
  17. function (newValue, oldValue){
  18. //console.log("elm.id =", elm.id);
  19. //The validation error message will be placed on the element 'title' attribute which will be the field 'tooltip'.
  20. //newValue == true means there is error
  21. if (newValue) {
  22. var msgVar1Val;
  23. //Perform variable substitution if required to get the final text of the error message.
  24. if (msgVar1) {
  25. msgVar1Val = scope.$eval(angular.element(elm).attr(msgVar1));
  26. errMsg = errMsg.format(msgVar1Val);
  27. }
  28. //Append the error to the title if neeeded
  29. if (elm.title) {
  30. elm.title += " ";
  31. } else {
  32. elm.title = "";
  33. }
  34. elm.title += errMsg;
  35. } else {
  36. //Remove the error if valid.
  37. //child.removeAttribute('title');
  38. if (elm.title) {
  39. //Replace the error message with blank.
  40. elm.title = elm.title.replace(errMsg, "").trim();
  41. }
  42. }
  43. });
  44. } else {
  45. //console.warn("Warning in addValidationMessage() for element ID '%s' in ngForm '%s'. Message: 'ng-model' is not defined.", elm.id, ngForm.$name)
  46. }
  47. }, 1000);
  48. }
  49.  
  50. errMsg = "Number of characters entered should not exceed '{0}' characters.";
  51. addValidationMessage(ngForm, child, 'maxlength', errMsg, 'ng-maxlength');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement