-
Notifications
You must be signed in to change notification settings - Fork 6.8k
mat-form-field does not remove mat-form-field-invalid class on FormGroup reset #4190
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
Comments
Have the same issue 😢 |
plunker repro: http://plnkr.co/edit/UguaoP1sTR85pyZLh2TN?p=preview |
It looks like this is just a matter of putting |
@mmalerba trying to understand how this would fix the issue for a button already type="submit". I'm resetting after the form is submitted and a response has been received back from the server in an observable complete block. i.e., |
@goelinsights sounds like maybe a forms issue (could be related to angular/angular#15741). The |
Not sure why, using resetForm() method fixed this issue for me. |
@mmalerba not sure i agree with your diagnosis. I am experiencing same issues and have only updated from material-beta2 to material-beta3. Main angular libraries remain unchanged and problem has only been introduced since material upgrade.
after form is submitted, error styling should definitely be removed. |
@mmalerba Why is this issue closed? The problem still exists when reseting the formGroup like explained by @goelinsights Scenario: |
@jeroenvanagt try this <form [formGroup]="myFormGroup" #f="ngForm">
...
</form> @ViewChild('f') myNgForm;
reset() {
this.myNgForm.resetForm();
} |
I understand that a reset button works. However, in my case I want to use a submit button, send the data to the backend server and then reset the form (so a new invite can be created). For the reset, I use : |
I now use the following workaround
|
@jeroenvanagt Error state is calculated like this: isInvalid && (isTouched || isSubmitted) Unfortunately, resetting the FormGroup isn't enough. You'll need to reset the submitted state of the actual form. To do that, you need to get access to the FormGroupDirective which binds the FormGroup to the form and then call <form [formGroup]="fg">
...
</form> @ViewChild(FormGroupDirective) myForm;
sendDataToBackendAndResetForm() {
// TODO: send data to backend
if (this.myForm) {
this.myForm.resetForm();
}
} See this updated plunker for an example |
@mmalerba I don't think we can call a workaround a resolution. This still is not fixed. Why can't the |
@Nathan-Ryan The |
@mmalerba the hacky workaround is just to manually remove .mat-input-invalid
on submission. If that's the desired approach, we should put it into the
docs as it's a fairly straightforward way to address what should be a
common problem for a reusable form that is reset on submission (that from
my perspective isn't an expected or obvious output).
…On Wed, Jun 21, 2017 at 12:01 PM, mmalerba ***@***.***> wrote:
@Nathan-Ryan <https://github.com/nathan-ryan> The FormGroup and
FormGroupDirective are part of angular core, not angular material. If you
want to request changes to the behavior you can file an issue with
https://github.com/angular/angular
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4190 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACv1DSsUuegP-lRLkdGOj5o1CoY8DEAUks5sGWh_gaJpZM4NDvLu>
.
--
-------------------------
Vijay Goel, M.D.
Principal, Goel Insights
310-492-5601
www.goelinsights.com
|
@goelinsights I may be mistaken but manually removing classes is not the recommended workaround. I'll summarize here:
|
Any Update on this fix? |
The simplest solution isn't resetting the form afterwards, but preventing the form from being submitted in the first place. You can do that by putting a
|
I think @biowaffeln's answer is the way to go. Simply avoid all references to "Submit" and you're good. To expand on his example: Before:
After:
Your submit function doesn't accept any arguments, it will simply access form values via the class property, so it doesn't make a difference either way:
|
@jplew an advantage of using (ngSubmit) on form is that the form is automatically submitted when you press Enter in a text field. You would need to do that manually. |
I think resetting the directive should be the default behavior of a form field. The issue is open for 3 years, is there any progress so far? |
TL;dr: use So, I was feeling pretty salty about this behavior. Why is this clearly wrong yet nobody is budging? It seemed to me like one of the following should be true:
Well, this is just one of those things where everyone is kinda doing the right thing. If you wound up here, what nobody's saying is that the DOM is crazy and your HTML is wrong (MDN):
All of that is to say, if you are using Now, why is your HTML wrong? Three reasons:
The FormGroup really has no idea about any of this because the FormGroup is a pure Angular construct that has nothing to do with DOM APIs. The FormGroupDirective knows about it because it's the jerk that actually talks to the DOM. And now our helpful little mat-form-field comes a long, brings them together, and tells us the form is invalid, which is true! That happened! Yet nobody knows about that So, if you can't add
That still doesn't solve the Enter problem, so either handle that as well or just use Okay, what if you really want to lean on the native |
I have tried many methods, include this worked for me! 😄 but why ? 🤔 |
I think (and I'm honestly not positive, I'm still learning a lot of this) the $event of ngSubmit is a wrapper to prevent the html form element's default submit event from firing and triggering an HTTP post. currentTarget is the HTML form, and uses the DOM reset() rather than the FormGroup reset(). |
I can't believe the issue is still open from 2017 to this day! |
For me, the simplest solution was the following: I added variable - submitted to each error in my html file like this and set it initially as false in my .ts file: So in order for the error to be shown, I need to submit my form. When i do submit it and I get the response from the backend, I set it back to false and the errors are gone. |
Just use (if you can) <div [fomGroup]="form"> instead of <form [fomGroup]="form"> |
@Lucas00012 just be aware this takes away other functionality such as validation on submit. |
The FormGroupDirective is subscribed to the reset event, so when you call reset on the form element it calls the same resetForm function that willshowell referred. #4190 (comment) |
Any update on this issue? I'm still facing this problem. Unfortunately, none of the workarounds worked for me. I reset a form group based on changes made to the control in the parent form group in my component. Upon reset, the values are getting reset, but the material select dropdown still shows invalid. I tried all the combinations like reset(), setErrors(null), updateValueAndValidity() etc., but no luck. |
@GowthamSankaran that's unfortunate, although the workarounds are working for me. |
none of the answers here worked for me, but plain html/javascript worked, it's a shame bc, why would plain html do something Angular can't?
|
this bug still exist until now (angular 12) any updates or idea to solve this issue...
not a good practice but useful, |
The error is still present today? |
Yup the error is still present and got caught by it today. After googling around, end up here. I used the suggestion |
Still facing this issue, reset() wont reset the validation. resetForm() on the directive doesn't solve my issue, however the Object.keys setErrors above works.. Which is redundant as f*** for all of my different components. |
I'm also facing this error. |
Anybody knows if the new form (v14) fix this issue? |
The solution is simple here. Add this to your module:
Then you can simply use |
In v13 , setting button type as "button" worked for me. |
Hey everyone Anything new? Still need to call
|
The best solution if you want to reset the form as a side-effect (like after http request), thank you! Material Possible (great) solutions - #4190 (comment) |
Perfect example of Occam's Razor: The simplest explanation is always the best. |
2024 and I need to still look for a workaround to this? :( |
What works in 2024 with the least extra codeWith Angular 18 and reactive forms and standalone components and Material 3, the one that still seems to work is to setup your form like
The What also worksYou can also do:
I kind of like this one because it's clear that the reset takes two steps working on two different parts of the form system, though it's still a workaround for a single form reset. The downside is the mess is spread to both the HTML template and the submit processing code. What doesn't workThere were a number of ideas that probably worked in the past that I tried that do not seem to work today:
|
I thinked that the problem was I. This error is a lot of complex. I see that if in the button use type="Submit" when type script registerForm.reset() execute the validation and show errors but if button is type="Reset" or (click)=funcion() the type script ejecuta registerForm.reset() without problem. Someone presented one solutión that do the thing good but don't know if is best practic. `<form [formGroup]="registerForm" (submit)="submitF($event)"> submitF(event:any){ This should be more easy the implement of shape nativa. |
My solution was create two buttons one whit submit for send information and other for reset the data of form.
this not is one solution elegant but for moment work well |
The thing is that wath @jpangburn suggest is basically wath should do with the form from the formBuilder. Why is not doing it ? I think it's redundant, but it's necessary to doit, because in the other way, it does not work correctly. |
Uh oh!
There was an error while loading. Please reload this page.
Bug, feature request, or proposal:
Bug
What is the expected behavior?
Upon resetting a FormGroup, I would expect the visual state of the form to be reset also.
What is the current behavior?
The form group and controls are reset but the mat-input-invalid class remains on the md-input-container suggesting that the control is invalid.
What are the steps to reproduce?
This only seems to happen when the [formGroup] attribute is on a form tag. Works as expected on a div tag.
Create component with a FormGroup and a FormControl with Validators.required .
Create a form tag with the [formGroup] attribute.
Add an input control to the form with a required attribute.
When running, fill out the form so its valid, then reset the form and model through the click handler on a button. The required control will now have the mat-input-invalid class.
Which versions of Angular, Material, OS, browsers are affected?
Angular 4.0.1
Material 2.0.0-beta.3
Windows 10
Chrome 57
The text was updated successfully, but these errors were encountered: