Jakobovski / angular-validator

Powerful, flexible and simple Angular validation!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Having issues with custom validators on inputs inside ng-if

yoanpumar opened this issue · comments

Hello guys,
I'm getting this error "Cannot read property '$setValidity' of undefined". Am I missing something? Below is my code:

 <form name="vm.myForm" class="form-horizontal" angular-validator angular-validator-submit='vm.updateMatchScore()' novalidate>
<div class="form-group col-xs-12">
                        <div ng-repeat="item in vm.items track by $index" class="radio-inline">
                            <input type="radio"
                                   id="item_{{item.id}}"
                                   name="item_{{item.id}}"
                                   ng-value="{{item.id}}"
                                   ng-model="vm.myViewModel.itemId"
                                   ng-required="true">
                            <label for="item_{{item.id}}">{{item.description}}</label>
                        </div>
</div>

<div class="form-group col-xs-12" ng-if="vm.myViewModel.itemId === 1">
            <input type="text"
                        class="form-control"
                        name="myInput1"
                        ng-model="vm.myViewModel.myInput1Value"
                        ng-required="true"
                        min="0"
                        max="7"                                       
                        validator="vm.myCustomValidator(vm.myViewModel.myInput1Value)" />
</div>
<div class="form-group col-xs-12" ng-if="vm.myViewModel.itemId === 2">
            <input type="text"
                        class="form-control"
                        name="myInput2"
                        ng-model="vm.myViewModel.myInput2Value"
                        ng-required="true"
                        min="0"
                        max="7"                                       
                       validator="vm.myCustomValidator(vm.myViewModel.myInput2Value)" />
</div>

 <button type="submit" class="btn btn-primary">Submit</button>
</form>

It seems that it is just validating the inputs rendered at the very first and if that input is no longer in the DOM, because the ng-if condition is changed to false by the user interaction, it throw the exception when trying to $setValidity in an element that is not in the DOM. Has anyone had this issue before? Any help will be really appreciated!!

Thank you.

commented

It is a pretty old issue, but I just faced it now, my workaround could be helpful for someone else in the future:

angular.module('myModule')
  .directive('inputOnDestroyWatcher', [
    function () {
      return {
        restrict: 'A',
        link(scope, elem) {
          scope.$on('$destroy', () => {
            elem[0].removeAttribute('validator');
          });
        },
      };
    }
  ]);

Although ngIf removes the element from the DOM, as well as the related property from the form object, angularValidator still finds the DOM element somehow thus tries to retrieve and validate the property in the form object. As the property is already removed from the form object it will throw an undefined error. There is a condition in the source code which checks if the given DOM element has a validator property and if so it triggers the process. My solution is based on that condition, if we remove the validator property the condition will evaluate to false and nothing will be triggered. This is not the best solution, but can be a quick fix as this repo seems pretty abandoned. Maybe in the future I'll fork it and resolve the issue in the source code - if so I'll link it here.