Question: 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

Answer: 
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](http://jsfiddle.net/ata5q1vj/)**
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.