-
Notifications
You must be signed in to change notification settings - Fork 26.5k
feat(forms): add ability to reset forms #9974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
c37f2d4
to
13e34b2
Compare
eb5d218
to
822f8c6
Compare
@vkniazeu, this PR isn't the right place to report an issue with the docs examples. Please file an issue here: https://github.com/angular/angular.io. |
@@ -48,4 +48,8 @@ export abstract class AbstractControlDirective { | |||
} | |||
|
|||
get path(): string[] { return null; } | |||
|
|||
reset(value?: any): void { |
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.
Even though it doesn't matter, I think the intent is clearer when you use reset(value: any = undefined)
. The reason is you always use the value, the value is not optional. It is just you use 'default' when the value is not provided.
Also, I thought you were going to default to null, not to undefined.
Hoooraaay! 🍻 |
Hi - Can we have the documentation updated on how to reset using new forms? Do we have to use formgroup to reset pristine state? |
Hey, I don't know if I should open a new issue with a feature request for the following but I think it would be good to first talk about my idea here:
So in my opnion the following changes would be the best:
We can argue about naming or even different changes but atm it's just counterintuitive. |
@DeusProx To give you some background, the When you call So you can also do this: this.form.reset({
first: {value: 'Carson', disabled: true}
last: {value: 'Drew', disabled: false}
}) Also keep in mind that what you're suggesting would be a significant breaking change for users. I'd like to avoid breaking applications unless there's a compelling reason, and in this case, I think the existing behavior that mimics native forms makes more sense. That said, I can see where you're coming from and we always appreciate the feedback. |
@kara I understand your reasoning. Especially that you want to mimic a native reset button and use a common naming scheme. I surely can agree on that after your post! Still one should think about a functionality to fill in the default values automatically which we defined in the formgroup/formbuilder otherwise I don't even see a reason why we should specify them from the start. We could alternatively just specify the form and it's validators in the first step and then write a function which uses |
@DeusProx If you move the code that creates the |
@zoechi I know that. i recently read in another issue that this was the "common version" of a reset in earlier versions. I also thought of this but is this really a good solution? I don't know about any side effects or performance problems. Could there be problems? |
How many times per second do you plan to reset the form? ;-) |
Reseting the form in simple javascript is the solution for now. var form : HTMLFormElement = document.getElementById('id'); |
@zoechi right now I'm facing this. We are serving <= per second a new work to user after he/she submit current work. In this situation after loading the new values our form reset 👎 .
Any alternative way for this ? and even we validating only after |
@AlwaysAbhl001 I'd suggest to create a new issue with a StackBlitz/Plunker to reproduce. |
@AlwaysAbhl001 ignoring half of my comment won't help you. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This PR adds the ability to reset forms programmatically. Calling
reset()
at any level will:Usage
You can also call
markAsPristine()
ormarkAsUntouched()
individually at any level if you'd like more fine-tuned control.This PR also ensures that native reset buttons reset the form automatically. In other words, clicking the following button will reset all your form fields to null in both the UI and in your model: