Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

1.2.0-rc1 Clearing invalid form inputs #3885

Closed
dmitrybelyakov opened this issue Sep 5, 2013 · 2 comments
Closed

1.2.0-rc1 Clearing invalid form inputs #3885

dmitrybelyakov opened this issue Sep 5, 2013 · 2 comments

Comments

@dmitrybelyakov
Copy link

Hi!

Today I accidentally found out that that it is not possible to clear the form of any input if that input is invalid.

In my understanding the form does not populate bound model unless it is valid. This way you can't reset bound model and expect invalid inputs to disappear. Because they are saved within forms's $inputValue. Setting form to pristine does not help with that either.

I think it should be either a part of form controllers $setPristine or a dedicated method such as $clearValues. Otherwise there's no way to clear invalid input.

And a quick demo (just type anything < 3 characters and try to discard changes):

<form name="myForm" novalidate>
  <div>Your data: {{data.text}}</div>
  <input type="text" name="text" ng-model="data.text" ng-minlength="3">
  <div data-ng-show="myForm.text.$error.minlength">Too Short!</div>
  <br>
  <input type="submit" value="Submit">
  <input type="button" value="Discard" ng-click="cancel()">
</form>

and controller

//controller
$scope.data = {text: undefined};
$scope.cancel = function(){
  $scope.data.text = undefined;
  $scope.myForm.$setPristine();
};
@umurkontaci
Copy link
Contributor

It works if you set data.text = '' instead of undefined

http://plnkr.co/edit/MCRH4J?p=preview

@dmitrybelyakov
Copy link
Author

@umurkontaci

Nice one, thanks Umur :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants