Repository navigation
ngForm stays $dirty after control.$setPristine() #13715
Description
Activity
I'm pretty sure this is how it is supposed to work.
ngFormdoes not keep track of which controls are pristine/dirty. Setting a control to$dirtywill propagation the "dirty-ness" to the parent form controllers, but setting all controls to$pristinewill not set the parent form(s) to$pristineas well.It's not an unreasonable feature to request, but it would add a tiny bit of overhead (which won't be necessary for most apps).
If you need such functionality, you can always implement a work-around.
In any case, this will be much easier if/when we expose thecontrolsarray.@Narretz, do you think it is reasonable to check if all controls are
$pristine(whenever a control is set to$pristine) and set the parent form to$pristinetoo (if all controls are$pristine) ?User expectation is probably that the form is set to $pristine, and I can see how that makes sense. We have the same problem with $setValidity, too, afaik.
I'm not to keen about implementing this logic. A parent form form would potentially need to loop through hundreds of children to determine its pristine-ness.
Either we make this check available behind a flag, or we expose the child forms / child controls on the form element, so that developers can loop over them themselves.- added a commit that references this issue
on Jan 15, 2016 I wrote some brute force solution, but.. what if we will keep track of number of nested pristine controls/forms? 0 by default, it will be increased in
$setPristinand decreased in$setDirty. By comparing it each time withcontrols.lengthwe will get$pristine. I'm not sure what should happen when we add/remove controls, but i guess it can be solved too.Having an internal counter also seems like a viable solution. I can't think of a corner case that could not be handled with such an implementation (if implemented properly).
Hi, thanks for considering this.
I understand that revalidating the entire form to check for $pristine is a an inexpensive operation relative to setting the form to $dirty when a single control is set dirty. I like the idea of counters for the $touched, $untouched, $pristine and $dirty status of each field, since we only care whether or not the counter is 0 each.
- added 2 commits that reference this issue
on Jan 19, 2016 I take it this issue has never been resolved, and probably won't be?
Reacted by George Kalpakas@adamreisnz That's correct. It should be possible to implement this logic by using https://docs.angularjs.org/api/ng/type/form.FormController#$getControls and a custom $setPristine implementation.
Hi there. Frustrating problem. in an ngForm, if in my controller I set one of the form's controls to $dirty (via control.$setDirty()), then in the controller set the same control to $pristine, the form remains $dirty.
I'm pretty sure this shouldn't happen, and is getting kludgy to fix. I'm using Angular 1.4.8.
Plunkr:
https://plnkr.co/edit/6noEtj6vbMkqxiKK6Yp7?p=preview
Thanks