0

I have a form and want to test the validity of the form. I have a function in the controller that checks for $valid

$scope.checkValid = function(){
    console.log($scope.form.address)
    console.log($scope.form.address.$valid);
    if($scope.form.address.$valid){ 
       dosomething();
     }
 }   

The dosomething() does nothing!! The first console.log produces a Constructor object with the valid as true.

$valid: true

yet the second console log displays FALSE!

Why would this happen? I cant figure it out and google has no answers.

Any advice appreciated.

3
  • 1
    By "The dosomething() does nothing!!" you do mean "The dosomething() is never called!!" ? Commented Dec 17, 2014 at 13:35
  • 2
    could you create a jsfiddle for your problem Commented Dec 17, 2014 at 13:36
  • here is a basic jsfiddle jsfiddle.net/xyorzghz/14 As you can see in the html its true but in the controller it doesnt do the $scope.dosomething(); Commented Dec 17, 2014 at 14:01

1 Answer 1

1

You want to validate form validity, and thats variable "formName.$valid" in form´s scope.

You really should provide jsfiddle as others mentioned, because you didnt specified, where and how are you using this method checkValid();

You can see any validation errors in "formName.$error"

UPDATE on your fiddle:

You didnt set any watcher on that validity variable

Repaired working fiddle is here: Working example

_

Or, you can use "ng-change='inputChanged(event)' " on that input and check validity there.

Sign up to request clarification or add additional context in comments.

4 Comments

Apologies on that, I have one now in the above comments.
Thanks for the update. What I am trying to do though is evaluate the forms validity before the user inputs any information.
Thing is, that form is not loaded after controller starts, so you cannot get form in scope yet. But you can wrap check into timeout service, example: Fiddle
This was the exact answer I was looking for. Thank you. It all works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.