Lindo Sebastian
Lindo Sebastian

Reputation: 30

$setValidity is not working for my validation

I have a form called ganesConfig and based on some condition i want to show the error message.

<form method="post" name="gamesConfig" novalidate>
    <p ng-show="gamesConfig.selectedGames.$invalid.gamesduplicate">Already Exists. Please try another</p>
</form>

the condition is as follows

$scope.gamesConfig.selectedGames.$setValidity("gamesduplicate", false);

But not showing the error message.

Upvotes: 0

Views: 981

Answers (1)

ngCoder
ngCoder

Reputation: 2105

Here is a sample example I've made out from what you have provided.You didn't provide the 'name' attribute to the input field which takes the game value by which we decide the duplicates.

$scope.game = {};

     $scope.checkName = function() {
            if ($scope.game.name == 'Test') {
                $scope.gamesConfig.selectedGames.$setValidity("gamesduplicate", false);
            }
        };

Your HTML should look like below

<ng-form method="post" name="gamesConfig" novalidate>
    <input type="text" name="selectedGames" ng-model="game.name" ng-change="checkName()"/>
    <p ng-show="gamesConfig.selectedGames.$invalid">Already Exists. Please try another</p>
</ng-form>

Upvotes: 1

Related Questions