Skip to content

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

Open
@yoanpumar

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions