You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Form does not revert to initial state when submitted #1903
When I submit a form that I want to re-use and reset the inputs I expect 'ng-invalid-required' to no longer be set on the form. Instead it is still set and the form is shown as "red".
@andreabadesso the named property is based on the name property you give it. Give your form and each input in it a name, and then you can do the same thing.
I updated the code pen giving a name to the form and to the input and indeed setting $touched = false does work, except that it is not resetting the md-char-counter, is that a bug.
Also for some reason setting $touched = false does not work in my full environment, even though it does work in the code-pen, I'm not sure what might cause that.
By default, mdInputContainer displays its errors only if the containing input has been $touched (focused and blurred). (BTW, you can change this default behaviour, with the mdIsError attribute.)
After you submit your form, and reset/empty your model, the fields are still $touched, thus errors will be displayed.
If you want to reset the whole form (i.e. all mdInputContainers in it), you can call $setUntouched() on the formController, as shown here.
mdMaxlength's not resetting the counter to 0, is indeed a bug with mdMaxlength (which needs some love anyway).
Some reset function would definitely be appreciable. Don't have time to figure out why but suggested workarounds here by @andreabadesso & @gkalpak didn't work.
I tried in the console to play with either the form or the input and $touched = false, $valid = true, $invalid = false (wondering btw why those 2 exist), $setPristline(), $setUntouched()... but didn't get any positive results.
I don't have time to dig more into this but that's annoying.
submit (it will reset the form)
expected: ng-messages also become hidden
actual: visible however the form field shows as a untouched (therefore valid) field
you most likely didn't import 'angular-messages' module. i was also having the same issue, but just adding that fixed everything. have a look at this https://scotch.io/tutorials/angularjs-form-validation-with-ngmessages. i know it's pure angular, but it works the same, no need to check $touched or any other sorcery.
This issue is closed as it falls in the 'deprecated' category. We deprecate issues for one or more of the following reasons:
Issue has a reasonable workaround
Issue doesn't have a demo or reproducible case available
Issue is real, but fixing it is risky for users who depend on existing behavior
Issue is real, but only affects a very small use case
Issue is real, but would require significant work to fix, and is less urgent than other needs.
Pull Requests are welcomed to resolve this issue. But note that not all community PRs will be merged into master. Pull Requests with large changes or large scope impacts will probably be rejected [due to stability risks to existing users].
Activity
andreabadesso commentedon Mar 14, 2015
You can workaround it by giving a name to the form and input and setting:
$touched = false;mxab commentedon Mar 14, 2015
Hey, I have the same problem,
but @andreabadesso the named property in my form does not seem to be there, so the $touched property is also not there
erikdonohoo commentedon Mar 15, 2015
@andreabadesso the named property is based on the
nameproperty you give it. Give your form and each input in it a name, and then you can do the same thing.axelson commentedon Mar 17, 2015
I updated the code pen giving a name to the form and to the input and indeed setting
$touched = falsedoes work, except that it is not resetting themd-char-counter, is that a bug.http://codepen.io/anon/pen/ogaROy
Also for some reason setting
$touched = falsedoes not work in my full environment, even though it does work in the code-pen, I'm not sure what might cause that.gkalpak commentedon Mar 19, 2015
Adding a little context:
By default,
mdInputContainerdisplays its errors only if the containing input has been$touched(focused and blurred). (BTW, you can change this default behaviour, with themdIsErrorattribute.)After you submit your form, and reset/empty your model, the fields are still
$touched, thus errors will be displayed.If you want to reset the whole form (i.e. all
mdInputContainers in it), you can call$setUntouched()on the formController, as shown here.mdMaxlength's not resetting the counter to 0, is indeed a bug withmdMaxlength(which needs some love anyway).12 remaining items
wembernard commentedon Apr 8, 2016
Some
resetfunction would definitely be appreciable. Don't have time to figure out why but suggested workarounds here by @andreabadesso & @gkalpak didn't work.I tried in the console to play with either the form or the input and
$touched = false,$valid = true,$invalid = false(wondering btw why those 2 exist),$setPristline(),$setUntouched()... but didn't get any positive results.I don't have time to dig more into this but that's annoying.
garaboncias commentedon Apr 19, 2016
form.$setUntouched() is works for input field's border but not for ng-messages:
http://codepen.io/anon/pen/MyXmpN
expected: ng-messages also become hidden
actual: visible however the form field shows as a untouched (therefore valid) field
jazdw commentedon Oct 6, 2016
Can we re-open this, its still broken in 1.1.1
Jay0328 commentedon Oct 15, 2016
I also still have this problem.
Hope to repair this.
dnsmob commentedon Oct 16, 2016
you most likely didn't import 'angular-messages' module. i was also having the same issue, but just adding that fixed everything. have a look at this https://scotch.io/tutorials/angularjs-form-validation-with-ngmessages. i know it's pure angular, but it works the same, no need to check $touched or any other sorcery.
ThomasBurleson commentedon Oct 17, 2016
This issue is closed as it falls in the 'deprecated' category. We deprecate issues for one or more of the following reasons: