after resetting angular form i am getting red invalid form

Solutions on MaxInterview for after resetting angular form i am getting red invalid form by the best coders in the world

showing results for - "after resetting angular form i am getting red invalid form"
Dior
12 Jun 2018
1By default, Angular/Material watch formControl's error state not only by touched but also submitted status of the form, see below source code.
2
3isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
4  return !!(control && control.invalid && (control.touched || (form && form.submitted)));
5                                                                       ^^^^^^^^^^^^^^
6}
7For reason above, you also need to reset the form to unsubmitted status.
8
9You can call resetForm of FormGroupDirective(getting instance by ViewChild) for it will reset both the form data and submit status.
10
11<form #form="ngForm" [formGroup]="checkForm" (ngSubmit)="submitForm(form)">
12  ...
13</form>
14
15@ViewChild('form') form;
16
17submitForm() {
18  this.form.resetForm();
19  ...
20}
21You can also overwrite the default one via implementing ErrorStateMatcher with custom conditions such as ignore submitted status of the form, and apply it on material components.
22
23<mat-form-field>
24  <input matInput formControlName="name" placeholder="name" [errorStateMatcher]="errorMatcher" />
25  <mat-error *ngIf="checkForm.get('name').errors?.required">
26    Name is required.
27  </mat-error>
28</mat-form-field>
29
30// define custom ErrorStateMatcher
31export class CustomErrorStateMatcher implements ErrorStateMatcher {
32  isErrorState(control: FormControl, form: NgForm | FormGroupDirective | null) {
33    return control && control.invalid && control.touched;
34  }
35}
36
37// component code
38@Component({...})
39export class CustomComponent {
40  // create instance of custom ErrorStateMatcher
41  errorMatcher = new CustomErrorStateMatcher();
42}
43see fixed demo.
44
45Share
46Improve this answer
47Follow
48edited Jan 29 '19 at 3:35
49answered Apr 12 '18 at 5:58
50
51Pengyy
5231.9k1515 gold badges6969 silver badges6868 bronze badges
53Thanks for the solution. In the return statement of isErrorState(), both touch and form submitted are connected by "or" operator so only one of them need to be true at one point of time. I think builtIn function "checkForm.reset()" internally untouched, pristine all the fields. – Ajay Apr 12 '18 at 7:54
541
55@Ajay Mention expression (control.touched || (form && form.submitted), either touched or form submitted will turn above expression to true. – Pengyy Apr 12 '18 at 8:19 
56I am facing issues with above code. It show fields after focussing on them. Any hint would be helpful. – Ajay Apr 17 '18 at 4:03 
57Fields are hidden in starting(similar to invisible). Shown only when user focusses on them. – Ajay Apr 17 '18 at 4:23
58@Ajay I didn't see it in the demo I provided. Maybe you forgot to set placeholder for them? – Pengyy Apr 17 '18 at 4:35
59Show 5 more comments
60
6116
62
63All you need to do is avoid (ngSubmit) method on your reactive form:
641. Remove (ngSubmit)="submitForm()" from your form tag
651. Change your form button type from 'submit' to 'button'
662. On this button you want to set a click action to (click)="submitForm()"
673. in the submitForm() method do:
68
69this.checkForm.markAsPristine();
70this.checkForm.markAsUntouched();
71This will submit the form and reset its value without alerting validators