thitemple
thitemple

Reputation: 6059

Determining if a form is valid using angular

I'm trying to determine if a form is valid or not using angular.

I have a form like this:

<form name="form" novalidate>
    <p>
        <label>Number: </label>
    <input type="number" min="0" max="10" ng-model="test.number" required />
    </p>
    <p>
        <label>Name: </label>
    <input type="text" ng-model="test.name" required />
    </p>
    <button ng-click="sendTest(test)">Submit</button>
</form>

And, on the sendTest function I did:

angular.module('demo', [
      ]).controller('MainCtrl', function($scope){
        $scope.test = {
          name: 'das'
        };

        $scope.sendTest = function(test) {
          console.log(form.$valid);
          console.log(test.$valid);
        }
      });

The problem is that both form.$valid and test.$valid are undefined. I tried to do this following these examples:

http://dailyjs.com/2013/06/06/angularjs-7/ http://www.youtube.com/watch?v=J82OD76QhPo

Here's the complete code for this demo: http://plnkr.co/edit/l0E62KPJu4Z2r15VNjJq

Upvotes: 1

Views: 7698

Answers (1)

Martin
Martin

Reputation: 8866

form gets added to the scope, try: console.log($scope.form.$valid)

BTW, it's called form because that's the value you have specified on the form tag's name attribute. You can also add name attributes to the input fields if you from your controller want to know the state of the specific fields.

Example: http://plnkr.co/edit/Ra98yIFYso94flDIqNCD?p=preview

Upvotes: 7

Related Questions