1

This is my form my HTML

        <form id = "myform" name="myform"  ng-submit="saveForm()" novalidate >

            <div class="input-group">
                <span class="input-group-addon"> <img src="/icon.png" alt=""/> </span>                    
                <input type="text" class="form-control" id="username" name="username" ng-model="username" placeholder="Username" autofocus required>                    
            </div>
            <span ng-show="formInvalid">Please enter username</span>


            <button type="submit" class="btn btn-default" id="saveBtn"> Save </button>
        </form>  

And inside the controller I have

$scope.formInvalid = false;

$scope.saveForm = function(){  
  if($scope.myform.username.$invalid){  
       $scope.formInvalid = true;
  }

if($scope.myform.$valid){
//....save it....

At first the form has no error message, if I hit "Save" the "Please enter username" appears, so far, all good.

But if I click on the form field to type a username, the error message does not go away. Even if I finish typing and click somewhere else, the error message still does not go away.

I also try

  if(!$scope.myform.username.$valid){  
       $scope.formInvalid = true;
  }

and I also try together

  if(!$scope.myform.username.$valid){  
       $scope.formInvalid = true;
  }

  if($scope.myform.username.$valid){  
       $scope.formInvalid = false;
  }

and the problem is still there. How can I debug? How do I fix this?

Thanks

3 Answers 3

3

You don't have to introduce and maintain a new variable ($scope.formInvalid) for managing the state of your form. Angular maintains the valid / invalid state of the form for you.

As your form is named myform, just show the message about the username based on the value of myform.username.$invalid, and save the form only if myform.$valid is true:

HTML

<span ng-show="myform.username.$invalid">Please enter username</span>

JS

$scope.saveForm = function () {
  if ($scope.myform.$valid) {
    // save the form
  }
};

See fiddle

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

3 Comments

Aha, so its really simple. Two things. 1- With this method, is there a way to begin with no message at all and show the message when field is empty and hit "Save"? I mixed ng-show/ng-hide with $valid/$invalid, but nothing. 2- The method I use in my original post is in the book "Beginning AngularJS", © 2014 by Andrew Grant, chapter 6. How come it is not working? I mean, its in a book, it should work, right? Thanks
1. Sure. When you hit enter, i.e. as soon as you submit the form, the flag myform.$submitted is set to true even if the form is invalid. So something like ng-show="myform.$submitted && myform.username.$invalid" should do the trick.
2. I don't know this book, but if the introduction of the variable formInvalid so as to show or hide a validation message comes from there, I think it's not a good advice. Basically the example you provided does not work because formInvalid is not set to false as soon as you start typing in the input.
0

you can try a watch event,

$scope.$watch('myform.$valid', function(n, o) {
if(n) {
$scope.formInvalid = false;
} else {
$scope.formInvalid = true;
}
});

But i might even be a better idea, if you start using validators.

2 Comments

What do you mean by "validators"? Can you give me an example? Thanks
0

you do not trigger a change to form invalid property anywhere, I suggest you solve this issue with angulars built in validators and ng-messages module, which will listen to changes on you're form inputs and notify when the inputs are valid or invalid and notify the warning text. Another approach you can take is use the ng-change directive on the inputs you want to listen to changes in and trigger and update on the form invalid property according to the inputs validity.

example : (taken from the official angular website )

<form name="myForm">
  <label>
    Enter your name:
    <input type="text"
           name="myName"
           ng-model="name"
           ng-minlength="5"
           ng-maxlength="20"
           required />
  </label>
  <pre>myForm.myName.$error = {{ myForm.myName.$error | json }}</pre>

  <div ng-messages="myForm.myName.$error" style="color:maroon" role="alert">
    <div ng-message="required">You did not enter a field</div>
    <div ng-message="minlength">Your field is too short</div>
    <div ng-message="maxlength">Your field is too long</div>
  </div>
</form>

i think this is the most elegant way to do it.

2 Comments

Can you give me an example on how to use validators and ng-messages? Thanks
i just edited my original answer with an example, take a look.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.