Bug
after resetting the formGroup the error associated with md-select should disappear
after resetting the formGroup the error associated with the md-select still displayed
Providing a StackBlitz/Plunker (or similar) is the best way to get the team to see your issue.
Plunker starter (using on @master): https://goo.gl/DlHd6U
StackBlitz starter (using latest npm release): https://goo.gl/wwnhMV
1) Create a formGroup with an md-select that is required for example
2) Reset the form after submission
Angular 4.3.3
Material 2.0.0-beta.11
Windows 10
Typescript 2.5.2
Browser Google Chrome 61.0.3163.100
In order to reset the formGroup i use the technique described here : https://github.com/angular/material2/issues/4190
Thanks.
I think this is expected behavior, per #4190.
After submitting the form and resetting it, the intuitive thing is to clear md-select from all sort of errors; Is there a good approach to achieve this ?
Thank you very much.
After you reset the form, that _is_ what happens. You are asking that by resetting your FormGroup, the FormGroupDirective is also reset. FormGroup and FormGroupDirective aren't coupled in that way due to whatever design decisions were made in @angular/forms.
--
https://github.com/angular/material2/issues/4190#issuecomment-311488176 pretty much covers it:
Submitted state is based on the
FormGroupDirectiveor theNgForm(depending on if you're using template- or model-driven forms).FormGroupdoes not hold submitted state.If you wish to reset the form, you must gain access to the
FormGroupDirectiveor theNgFormand callresetForm()on it. That will remove.mat-input-invalidfrom the input and all associatedmd-errorsThis behavior is in Angular core, not material. You are welcome to propose a change to the behavior, but that should be done in the angular core repository, not here.
If you simply don't want
.mat-input-invalidto depend at all on submitted state, you will be able to purpose https://github.com/angular/material2/pull/4750 for that once it lands.
I think there is a misunderstanding, i understand that in order to remove all errors after submission we need to gain access to FormGroupDirective or the NgForm and call resetForm() on it, but the problem is that just remove .mat-input-invalid from all inputs and not .mat-select.invalid from all selects components !
So my question is how to remove also all .mat-select.invalid from all selects components ?
Thank you very much and i'm sorry if i'm not getting something right.
I'm still not sure I understand 馃槃 . Here's an example of this approach clearing the invalid styles for inputs and selects:
https://stackblitz.com/edit/material2-beta11-8ylcrf?file=app%2Fapp.component.ts
And here's some additional clarification, if it helps:
By default, Material shows the invalid styling when the control isInvalid && (isTouched || isSubmitted). This strategy is recommended by the spec. If you want to customize how and when controls show invalid styling, you can use a custom error matcher (_the api does have a minor breaking change coming in beta.12_).
This is working as intended. As @willshowell mentioned, you can use a custom error state matcher to change when errors are shown
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
After you reset the form, that _is_ what happens. You are asking that by resetting your
FormGroup, theFormGroupDirectiveis also reset.FormGroupandFormGroupDirectivearen't coupled in that way due to whatever design decisions were made in@angular/forms.--
https://github.com/angular/material2/issues/4190#issuecomment-311488176 pretty much covers it: