-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Issue #856: add ability to reset a form to its pristine state #1127
Issue #856: add ability to reset a form to its pristine state #1127
Conversation
This would be nice, +1 :) |
OK, pushed another commit (df35bb7) with the discussed corrections. The only open question would be to keep both $setPristine / $setDirty methods or refactor it to sth else. Would be grateful for your input. |
Looks good! Can we merge this, @IgorMinar ? |
Thanks for your contribution! In order for us to be able to accept it, we ask you to sign our CLA (contributor's license agreement). CLA is important for us to be able to avoid legal troubles down the road. For individuals (a simple click-through form): For corporations (print, sign and scan+email, fax or mail): |
@mhevery I've already signed the CLA during the previous contribution (Pawel Kozlowski, [email protected]). From what I understand there is no need to sign it again, right? |
Random suggestion, maybe you guys should make an org team for people who have already signed? Don't necessarily have to give them any permissions whatsoever, but it would be a convenient and accessible way to see if they are already okay'd |
Don't forget this nice enhancement! |
Really, really, really want this feature. Any news on when it's expected to be approved and which version it'll ship in? Seeing as it was ready for approval over 2 months ago, this is kind of stretching out a bit ;o) |
+1 for releasing this issue. it is the last outstanding feature i need for an app i'm developing. |
Perhaps I am misunderstanding this but the proposed fix as shown in http://jsfiddle.net/pkozlowski_opensource/2x8HD/4/ ... doesn't 'reset/rebind' the input field. The value in the input control doesn't get reset/rebound to the value in ng-model and keeps whatever value you have entered. I was expecting the value in all the controls in the form to 'rebound' to the appropriate values as per their ng-model attribute. |
@rbygrave Rob the goal of this PR is not to change model values but just change form's state. You still need to change model values like so: http://jsfiddle.net/UAMzA/4/ BTW: to see why this PR is really needed try this jsFiddle and reset a value: http://jsfiddle.net/UAMzA/5/ |
form.$dirty = false; | ||
form.$pristine = true; | ||
angular.forEach(form, function(value, key) { | ||
if (value.$name && value.$setPristine) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you check for $name? anonymous form controls should be reset as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that anonymous form controls are not tracked as part of the form controller. Looked at the $addControl
method:
https://github.com/angular/angular.js/blob/master/src/ng/directive/form.js#L64 and the same check for $removeControl
:
https://github.com/angular/angular.js/blob/master/src/ng/directive/form.js#L70
If I'm correct then we don't have anonymous controls (inputs and sub-forms) on the FormController level and thus can't reset them to their pristine state. Still individual controls can be reset.
We could relay on the presence of the $setPristine
function only but somehow it didn't feel "right". Let me know if you want me to remove this test for the $name
property. In any case it won't change the fact that we can't (?) reset anonymous controls from a form level.
@IgorMinar might have missed something here but at least this was reasoning.
@IgorMinar Thnx for looking into this one! I'm going to swap the |
so I worked on this on the plane and found several issues - mainly that anonymous form controls and nested forms were not supported at all. I fixed these problems and added more tests. please see: https://github.com/IgorMinar/angular.js/commits/merge-pr-1127 if you are ok with these changes, I'll squash them and merge them. |
@IgorMinar I had a look and it looks good - at least as far as I can tell :-) Anyway, created a jsFiddle, it works OK: http://jsfiddle.net/Z9s3T/ Thnx once again for looking into this one! |
landed as 733a97a |
W00t! On 26 November 2012 15:49, Igor Minar [email protected] wrote:
|
This is a fix for the (quite up-voted now) issue #856. The whole commit boils down to adding the $setPristine() method to both ng.directive:form.FormController and ng.directive:ngModel.NgModelController; tests are added as well.
Here is a jsFiddle that shows an issue: http://jsfiddle.net/pkozlowski_opensource/qT9pu/2/ (just type sth in a field and then delete it).
Here is another jsFillde with the issue fixed: http://jsfiddle.net/pkozlowski_opensource/2x8HD/4/ (just type sth in a field and then delete it and click on 'Revert edits').