Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having issues with custom validators on inputs inside ng-if #135

Open
yoanpumar opened this issue Feb 25, 2018 · 1 comment
Open

Having issues with custom validators on inputs inside ng-if #135

yoanpumar opened this issue Feb 25, 2018 · 1 comment

Comments

@yoanpumar
Copy link

yoanpumar commented Feb 25, 2018

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.

@beahuszar
Copy link

beahuszar commented Nov 4, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants