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

Issue #856: add ability to reset a form to its pristine state #1127

Closed

Conversation

pkozlowski-opensource
Copy link
Member

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').

@ajoslin
Copy link
Contributor

ajoslin commented Jul 8, 2012

This would be nice, +1 :)

@pkozlowski-opensource
Copy link
Member Author

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.

@btford
Copy link
Contributor

btford commented Jul 19, 2012

Looks good! Can we merge this, @IgorMinar ?

@mhevery
Copy link
Contributor

mhevery commented Sep 12, 2012

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):
http://code.google.com/legal/individual-cla-v1.0.html

For corporations (print, sign and scan+email, fax or mail):
http://code.google.com/legal/corporate-cla-v1.0.html

@pkozlowski-opensource
Copy link
Member Author

@mhevery I've already signed the CLA during the previous contribution (Pawel Kozlowski, pkozlowski.opensource@gmail.com). From what I understand there is no need to sign it again, right?

@ProLoser
Copy link
Contributor

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

@petebacondarwin
Copy link
Member

Don't forget this nice enhancement!

@ghost
Copy link

ghost commented Oct 30, 2012

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)

@rtcarlson
Copy link

+1 for releasing this issue. it is the last outstanding feature i need for an app i'm developing.

@rbygrave
Copy link

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.

@pkozlowski-opensource
Copy link
Member Author

@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/
You will see that the input still has "invalid" classes even if we want to "reset" it for the next model entry.

form.$dirty = false;
form.$pristine = true;
angular.forEach(form, function(value, key) {
if (value.$name && value.$setPristine) {
Copy link
Contributor

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.

Copy link
Member Author

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.

@pkozlowski-opensource
Copy link
Member Author

@IgorMinar Thnx for looking into this one! I'm going to swap the angular.forEach for forEach, squash commits, rebase etc. Please let me know if you want me to remove the check for $name (see my in-line comments).

@IgorMinar
Copy link
Contributor

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.

@pkozlowski-opensource
Copy link
Member Author

@IgorMinar I had a look and it looks good - at least as far as I can tell :-)
I totally missed that nested forms are anonymous most of the time :-( Should have added tests for this....

Anyway, created a jsFiddle, it works OK: http://jsfiddle.net/Z9s3T/

Thnx once again for looking into this one!

@IgorMinar
Copy link
Contributor

landed as 733a97a

@IgorMinar IgorMinar closed this Nov 26, 2012
@petebacondarwin
Copy link
Member

W00t!

On 26 November 2012 15:49, Igor Minar notifications@github.com wrote:

landed as 733a97ahttps://github.com/angular/angular.js/commit/733a97adf87bf8f7ec6be22b37c4676cf7b5fc2b


Reply to this email directly or view it on GitHubhttps://github.com//pull/1127#issuecomment-10720313.

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

Successfully merging this pull request may close these issues.

None yet

10 participants