-
Notifications
You must be signed in to change notification settings - Fork 25k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reset FormGroupDirective when FormGroup.reset() is called #30987
Comments
We have the same problem with receiving submit/reset events on child form group controls and dirty state error handling which relies on either the control being dirty or formGroupDirective.submitted = true. We were able to work around receiving the submit event by listening to the parent ngSubmit event:
but unfortunately no such event exists for reset, specifically when formGroupDirective.resetForm() is called on the parent. This issue comes to light when working with an 'add + another' scenario where the 2nd and subsequent child form components are always in error state since their formGroupDirective.submitted is always true after the first form submission. What would solve this issue, aside from auto-propagation of the submitted property on child formGroupDirectives, would be to have a formGroupDirective.ngReset event emitter that we could hook into. |
Is there an update on this? |
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage. We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package. You can find more details about the feature request process in our documentation. |
I, for one, genuinely thought that more people would want this, judging by stackoverflow... So should I close this issue, or is it possible to ask to start voting again in the next development iteration (by which time hopefully it might get more votes?) |
Getting insufficient votes does not automatically mean it will be closed. But it is a signal that it may not have enough benefit to the community to warrant the effort to implement. In this case, since the votes are quite close to 20, I would expect that it will be included for consideration. |
I've added it to our triage board for future investigation and consideration. We'd need to do some additional work to fix this:
|
I'll see if I can get the stackblitz repo up and and running Update 1: Stackblitz seems to have some trouble with the new way material theming works (sass api)
Update 2: The formgroup as defined inside the component class (ts file) seems to be fixed now. It resets when programmatic reset is triggered. But unable to verify if that state is propagated to angular material. I think it will be best to provide a github repo instead while Stackblitz does not support angular material. This will allow us to then decide if this is a core angular issue or an issue with angular material |
Here is the stackblitz repo, with materia; working partially https://stackblitz.com/edit/angular-ivy-4phat8 It seems that the issue is now fixed???? - in angular and material version 12.1.2 (Can't confirm because we don't know if the control remains in warn-colour, even though the mat-error disappears) I think I shall go back and look at some of my actual projects after updating them, and report back. |
@DibyodyutiMondal Thanks for the contribution! Let us know what you find please :) |
FYI I'm closing this issue for now. Please reopen or create a new one if the problem still exists. Thank 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. |
馃殌 feature request
Relevant Package
This feature request is for @angular/formsSpecifically, ReactiveForms
Description
If a FormGroup is reset by calling
myFormGroup.reset()
in the typescript file , the related FormGroupDirective is not reset, in the sense, 'mat-errors' are still displayed, as if the controls were not pristine.Use case: If a field ("id" in this case) is required, on page load, the control is white, even if the validity is 'INVALID'. Once I submit a valid response, I process the response and then I reset the FormGroup, but the required field is now red and asking for input.
PS: There are a lot of related issues. In fact, there are so many, that I could not decide where to put this in. For Ex. #10999 was closed because of lack of use case, but I have given one here. Also in #15741, it is additionally mentioned that this problem particularly comes to the fore in case of asynchronous operations, which in my case, is also coupled with a lot of '*ngIf' templates.
Describe the solution you'd like
FormGroup could emit an event when it is resetting. and then the form group would subscribe to it inside ngOnInit(), and call resetForm() on itself every time the event is emitted.
Describe alternatives you've considered
One of the current workarounds is to take a ViewChild of the NgForm, like so
and every time I reset the FormGroup, to call resetForm on the ViewChild.
But this is tedious in the sense that the programmer must introduce new variables both in the template and typescript, and remember to call resetForm on the directive along with reset on the FormGroup/
The text was updated successfully, but these errors were encountered: