This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
ngForm stays $dirty after control.$setPristine() #13715
Closed
Description
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.
- on state load, form is $pristine
- via another event, call controller function that does control.$setDirty() - form is now $dirty
- via another event, call controller function that does control.$setPristine() - form is still $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
Activity
gkalpak commentedon Jan 9, 2016
I'm pretty sure this is how it is supposed to work.
ngForm
does not keep track of which controls are pristine/dirty. Setting a control to$dirty
will propagation the "dirty-ness" to the parent form controllers, but setting all controls to$pristine
will not set the parent form(s) to$pristine
as 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 the
controls
array.@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$pristine
too (if all controls are$pristine
) ?Narretz commentedon Jan 11, 2016
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.
fix(form): make ngForm $pristine after nested control.$setPristine()
linoleum-js commentedon 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
$setPristin
and decreased in$setDirty
. By comparing it each time withcontrols.length
we will get$pristine
. I'm not sure what should happen when we add/remove controls, but i guess it can be solved too.gkalpak commentedon Jan 15, 2016
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).
jar240 commentedon Jan 15, 2016
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.
fix(form): fix(form): make ngForm $pristine after nested control.$set…
fix(form): make ngForm $pristine after nested control.$setPristine() …
4 remaining items