You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mat-form-field does not remove mat-form-field-invalid class on FormGroup reset #4190
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
Reacted by Pavlo Kurochka, Daniel Haupt, Phil Targun, Jon Zgoda, Vijay Goel, TS, Juan Carlos Garibay, Ann Prokhorova, cristianqr, cedvdb and 74 more
Reacted by Ben, Filip Čulig, Stephanie McNaught and Just'in
changed the title [-]md-input-container does not remove mat-input-invalid class on FromGroup reset[/-][+]md-input-container does not remove mat-input-invalid class on FormGroup reset[/+]on Apr 21, 2017
It looks like this is just a matter of putting type="reset" on your button. Without it, the form is marked as submitted which causes the error styling to show up. see: http://plnkr.co/edit/IUhbPom8FnauivHUW3bU?p=preview
Reacted by Paul Robert W., Victor Redondo, Ignacio Íñigo Hernández, McGiogen, shivaspk, lianmaung, Cyrus Frost, Srdan Topalovic, shanti-unique, Ed Amor and 12 more
Reacted by Paul Robert W., Cyrus Frost, Srdan Topalovic and Clevanilson da Silva Oliveira
Reacted by Cyrus Frost, Clevanilson da Silva Oliveira and Bilal Mehrban
Reacted by Paul Robert W., Douglas Lira, Victor Redondo, Danilo, GanganiSon97, Cyrus Frost, Cédric Schweizer, Sreekar Swarnapuri Pandadu, Ed Amor, Ash Midgley and 4 more
Reacted by Cyrus Frost, Srdan Topalovic, ivanavukeljic, ✅ Daniel Danielecki, Clevanilson da Silva Oliveira, Bilal Mehrban and Mox-Evince
@goelinsights sounds like maybe a forms issue (could be related to angular/angular#15741). The md-input-container logic is correct, so there's nothing for material to do here
@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.
It looks like this is just a matter of putting type="reset" on your button. Without it, the form is marked as submitted which causes the error styling to show up.
after form is submitted, error styling should definitely be removed.
Reacted by Liad Idan, Mustafa Ekim, Ismael Ramos, Mohammad, Alain Kaiser, Mike P, Javier Cáceres, nitin, Darshana Nuwan Hettiarachchi, Patrick Pulwey and 75 more
Reacted by Hitesh Garg, Vijay Parne, Shailesh, Mohammad Sharifi, Adrián Caballero, Chad Kuehn, Dmitry Urenev, Eugen Sunic, rajathota1, soyoboyo and 16 more
Reacted by Raul Rosa da Silva, Mohammad Sharifi, Javi MB, jhhoff02, Atanas Drenovichki, man Q, Billy Lando and Auna Walton
Reacted by Ismael Ramos, Alain Kaiser, Javier Cáceres, Darshana Nuwan Hettiarachchi, Patrick Pulwey, Ted Mader, Laszlo Lang, Juliano Brasil, Raul Rosa da Silva, Ben Steward and 13 more
Reacted by Eugen Sunic, Michael Sanford, ChrisGi, Bruno Kremer, richardsolis, orazios98 and coopsana
Reacted by Ismael Ramos, Alain Kaiser, nitin, Darshana Nuwan Hettiarachchi, Patrick Pulwey, pwejnar, Juliano Brasil, Raul Rosa da Silva, Ben Steward, Mohammad Sharifi and 17 more
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 : this.form.reset()
where form is a formGroup
Reacted by Alejandro Cárdenas Islas Bustamante, Daniel, Michael Sanford, Bruno Kremer, Clark, Balint Csaszar (Chassy), Norbert Bartko, Paritosh, Dilini Peiris and Rico
Reacted by RaulLimaAlmira, Norbert Bartko and Aleksey Yeremeyev
Reacted by Rodrigo, David DeRemer, Richard Sithole, Ben, rachid O, vmvini, Patrick Christensen, sergii-danyliuk, Alejandro Cárdenas Islas Bustamante, AlonHmdt and 45 more
Reacted by Hitesh Garg, Javier Cáceres, Lizandro Zerpa, Gerson Pineda, Mark Kvetny, Shailesh, Maro, Andrey Korovin, Pavel Golodoniuc, Tommaso Galatolo and 24 more
Reacted by Narayan Sharma
Reacted by Prakhar Kothari, Emad Kheiralla, Harendra Kumar Kanojiya, Digamber, Alexander Spies, su, michalinasowamsi, Thibault Walterspieler, RaulLimaAlmira, stable-ellen and 8 more
Reacted by Oleh, andrew and Lovepreet Singh (Akaaljot)
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.
It looks like this is just a matter of putting type="reset" on your button. Without it, the form is marked as submitted which causes the error styling to show up. see: http://plnkr.co/edit/IUhbPom8FnauivHUW3bU?p=preview
Perfect example of Occam's Razor: The simplest explanation is always the best.
With Angular 18 and reactive forms and standalone components and Material 3, the one that still seems to work is to setup your form like newGateForm = this.formBuilder.group({... then in your submit handler after you've done your submit work:
// reset the form
this.newGateForm.reset();
// this hack is needed to clear any validation errors that are still showing
// really Angular? 7 years and no clean solution?
Object.keys(this.newGateForm.controls).forEach(key => {
//@ts-ignore
this.newGateForm.controls[key].setErrors(null);
});
The //@ts-ignore is because you're probably also using TypeScript in a new project and it doesn't like that line. I tried this on Chrome and Safari, both worked. I like this one because there's no changes to the HTML template, just the submit processing code so it keeps the mess contained to one dirty spot on the rug.
What also works
You can also do:
<form [formGroup]="myForm" #formDirective="ngForm" (ngSubmit)="submitForm(formDirective)">
and
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 work
There were a number of ideas that probably worked in the past that I tried that do not seem to work today:
<form novalidate... this would have been nice but doesn't seem to help.
<button type="reset" or <button type="button" this prevents your users from using the Enter key to submit forms. Users on non-touch devices will despise you for doing this. Do not do this. Always use <button type="submit" as the submit button on a form.
<div [formGroup]="form" same problem as reset. Don't do this to your users.
(ngSubmit)="onSubmitFunction($event)"> followed by the submit handler calling event.currentTarget.reset(). At least on my system, currentTarget was null so this didn't work.
@ViewChild(FormGroupDirective) myForm; then this.myForm.resetForm(). TBH, I didn't really understand how to use this, and the plunker was old enough that it didn't work. It might work if you knew how that directive was supposed to tie to your form.
Reacted by Jits, Ricardo Chavarria, Martynas Jakučionis, Bhautik Jivani, Szabolcs Kurdi, asevila, Rico and Cesar Vargas
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.
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.
With Angular 18 and reactive forms and standalone components and Material 3, the one that still seems to work is to setup your form like newGateForm = this.formBuilder.group({... then in your submit handler after you've done your submit work:
// reset the form
this.newGateForm.reset();
// this hack is needed to clear any validation errors that are still showing
// really Angular? 7 years and no clean solution?
Object.keys(this.newGateForm.controls).forEach(key => {
//@ts-ignore
this.newGateForm.controls[key].setErrors(null);
});
The //@ts-ignore is because you're probably also using TypeScript in a new project and it doesn't like that line. I tried this on Chrome and Safari, both worked. I like this one because there's no changes to the HTML template, just the submit processing code so it keeps the mess contained to one dirty spot on the rug.
What also works
You can also do:
<form [formGroup]="myForm" #formDirective="ngForm" (ngSubmit)="submitForm(formDirective)"> and
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 work
There were a number of ideas that probably worked in the past that I tried that do not seem to work today:
<form novalidate... this would have been nice but doesn't seem to help.
<button type="reset" or <button type="button" this prevents your users from using the Enter key to submit forms. Users on non-touch devices will despise you for doing this. Do not do this. Always use <button type="submit" as the submit button on a form.
<div [formGroup]="form" same problem as reset. Don't do this to your users.
(ngSubmit)="onSubmitFunction($event)"> followed by the submit handler calling event.currentTarget.reset(). At least on my system, currentTarget was null so this didn't work.
@ViewChild(FormGroupDirective) myForm; then this.myForm.resetForm(). TBH, I didn't really understand how to use this, and the plunker was old enough that it didn't work. It might work if you knew how that directive was supposed to tie to your form.
For me the first workaround will hide the errors but on a new submission the validation rules will be ignored and the formgroup will always be valid.
Activity
[-]md-input-container does not remove mat-input-invalid class on FromGroup reset[/-][+]md-input-container does not remove mat-input-invalid class on FormGroup reset[/+]kurpav commentedon Apr 22, 2017
Have the same issue 😢
mmalerba commentedon May 10, 2017
plunker repro: http://plnkr.co/edit/UguaoP1sTR85pyZLh2TN?p=preview
mmalerba commentedon May 11, 2017
It looks like this is just a matter of putting
type="reset"on your button. Without it, the form is marked as submitted which causes the error styling to show up. see: http://plnkr.co/edit/IUhbPom8FnauivHUW3bU?p=previewgoelinsights commentedon May 12, 2017
@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.,
submitForm()
.subscribe(
res => store.dispatch(type: foo, res),
e => log(e),
() => form.reset()); <- still leaving the form inputs as invalid
mmalerba commentedon May 12, 2017
@goelinsights sounds like maybe a forms issue (could be related to angular/angular#15741). The
md-input-containerlogic is correct, so there's nothing for material to do herezpydee commentedon May 13, 2017
@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.
jeroenvanagt commentedon May 30, 2017
@mmalerba Why is this issue closed? The problem still exists when reseting the formGroup like explained by @goelinsights
Scenario:
Press submit button
Do some stuff with the data
Then call formGroup.reset()
formGroup.reset() does NOT remove mat-input-invalid class

willshowell commentedon May 30, 2017
@jeroenvanagt try this
http://plnkr.co/edit/sRCiYvRqGevK493w79A4?p=preview
jeroenvanagt commentedon May 31, 2017
@willshowell,
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 :
this.form.reset()where form is a formGroup
jeroenvanagt commentedon May 31, 2017
I now use the following workaround
52 remaining items
BrutalCSkakan commentedon Jan 11, 2022
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.
Burtan commentedon Mar 8, 2022
I'm also facing this error.
mrmokwa commentedon Mar 9, 2022
Anybody knows if the new form (v14) fix this issue?
paradox37 commentedon Mar 15, 2022
The solution is simple here. Add this to your module:
Then you can simply use
this.form.reset(), nothing else.rajmetti commentedon May 6, 2022
In v13 , setting button type as "button" worked for me.
danielHin commentedon Sep 1, 2022
Hey everyone
Anything new?
Still need to call
.setErrors()on the certain form field to get it back into pristine state after resetting the whole form group in v13this.myFormGroup.get('label').setErrors(null);ApplY3D commentedon Nov 24, 2022
The best solution if you want to reset the form as a side-effect (like after http request), thank you!
Material
ErrorStateMatcherbind onform.submitted- source.So, for multiple submitting, you should provide different error matcher.
Possible (great) solutions - #4190 (comment)
saimyousuf commentedon May 21, 2023
Perfect example of Occam's Razor: The simplest explanation is always the best.
petogriac commentedon Apr 25, 2024
2024 and I need to still look for a workaround to this? :(
jpangburn commentedon Jun 2, 2024
What works in 2024 with the least extra code
With Angular 18 and reactive forms and standalone components and Material 3, the one that still seems to work is to setup your form like
newGateForm = this.formBuilder.group({...then in your submit handler after you've done your submit work:The
//@ts-ignoreis because you're probably also using TypeScript in a new project and it doesn't like that line. I tried this on Chrome and Safari, both worked. I like this one because there's no changes to the HTML template, just the submit processing code so it keeps the mess contained to one dirty spot on the rug.What also works
You can also do:
<form [formGroup]="myForm" #formDirective="ngForm" (ngSubmit)="submitForm(formDirective)">and
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 work
There were a number of ideas that probably worked in the past that I tried that do not seem to work today:
<form novalidate...this would have been nice but doesn't seem to help.<button type="reset"or<button type="button"this prevents your users from using the Enter key to submit forms. Users on non-touch devices will despise you for doing this. Do not do this. Always use<button type="submit"as the submit button on a form.<div [formGroup]="form"same problem as reset. Don't do this to your users.(ngSubmit)="onSubmitFunction($event)">followed by the submit handler callingevent.currentTarget.reset(). At least on my system, currentTarget was null so this didn't work.@ViewChild(FormGroupDirective) myForm;thenthis.myForm.resetForm(). TBH, I didn't really understand how to use this, and the plunker was old enough that it didn't work. It might work if you knew how that directive was supposed to tie to your form.ph360 commentedon Aug 31, 2024
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){
event.currentTarget.reset();
this.addressForm.reset();
}`
This should be more easy the implement of shape nativa.
ph360 commentedon Aug 31, 2024
My solution was create two buttons one whit submit for send information and other for reset the data of form.
<button mat-raised-button color="accent" type="submit" [disabled]="!registerForm.valid" i18n>Create new account</button> <button mat-raised-button type="Reset" i18n>Reset</button>this not is one solution elegant but for moment work well
asevila commentedon Nov 22, 2024
jpangburn
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.
Ben555555 commentedon Mar 23, 2026
For me the first workaround will hide the errors but on a new submission the validation rules will be ignored and the formgroup will always be valid.