0

I have a custom validation like this:

app.directive('checkRfc', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attr, mCtrl) {
      function checkRfcValidation(value) {
        if (scope.clientForm.requireBill.$modelValue === "1" && value.length > 0) {
          mCtrl.$setValidity('rfcOk', true);
          //scope.examsForm.authorization.$setValidity("discountOk", true);
        }
        else {
          mCtrl.$setValidity('rfcOk', false);
          //scope.examsForm.authorization.$setValidity("discountOk", false);
        }
        return value;
      }

      mCtrl.$parsers.push(checkRfcValidation);
    }
  };
});

Works fine but I have to enter some character in the corresponding field, then the validation will be triggered. Is there a way to activate automatically the validation on form load?

gof
  • 25
  • 8

1 Answers1

0

You can write a function in link as below

ngModel.$render = function () {.....}

This function executes when the model is first rendered and when the model is changed from code.So it will solve your both the purpose of triggering while value change as well as while render. Hope it'll help.

  • Thanks for your answer. You mean to put the checkRfcValidation function inside ngModel.$render? Another question is if I do it this way using only ngModel.$render this gives me the following error message: ngModel is undefined – gof Apr 30 '17 at 01:39
  • yes you can call you validation while model is rendering. For error you have to insert it in link somting like "link: function (scope, element, attr, ngModel) { }". Now ngModel is defined. – pragya sharma May 03 '17 at 06:31