Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 FormGroup reset doesn't reset validators

I have a form on my page and when I call FormGroup.reset() it sets the forms class to ng-pristine ng-untouched but FormControl.hasError(...) still returns truthy. What am I doing wrong here?

Template

<form [formGroup]="myForm" (ngSubmit)="submitForm(myForm)">   <mat-form-field>     <input matInput formControlName="email" />     <mat-error *ngIf="email.hasError('required')">       Email is a required feild     </mat-error>   </mat-form-field>   <mat-form-field>     <input matInput type="password" formControlName="password" />     <mat-error *ngIf="password.hasError('required')">       Password is a required feild     </mat-error>   </mat-form-field>   <button type="submit">Login</button> </form> 

Component

export class MyComponent {   private myForm: FormGroup;   private email: FormControl = new FormContorl('', Validators.required);   private password: FormControl = new FormControl('', Validators.required);    constructor(     private formBuilder: FormBuilder   ) {     this.myForm = formBuilder.group({       email: this.email,       password: this.password     });   }    private submitForm(formData: any): void {     this.myForm.reset();   } } 

Plunker

https://embed.plnkr.co/Hlivn4/

like image 597
efarley Avatar asked Jan 11 '18 21:01

efarley


People also ask

How do I remove all validators?

Either first use “clearValidators()” to remove all validators and then use “setValidators()” to set needed validation.

What does FormControl Reset do?

reset()linkResets the form control, marking it pristine and untouched , and resetting the value.

Is it possible to define validators in FormBuilder?

FormBuilder allows us to explicitly declare forms in our components. This allows us to also explicitly list each form control's validators.


2 Answers

It (FormGroup) behaves correctly. Your form requires username and password, thus when you reset the form it should be invalid (i.e. form with no username/password is not valid).

If I understand correctly, your issue here is why the red errors are not there at the first time you load the page (where the form is ALSO invalid) but pop up when you click the button. This issue is particularly prominent when you're using Material.

AFAIK, <mat-error> check the validity of FormGroupDirective, not FormGroup, and resetting FormGroup does not reset FormGroupDirective. It's a bit inconvenient, but to clear <mat-error> you would need to reset FormGroupDirective as well.

To do that, in your template, define a variable as such:

<form [formGroup]="myForm" #formDirective="ngForm"    (ngSubmit)="submitForm(myForm, formDirective)"> 

And in your component class, call formDirective.resetForm():

private submitForm(formData: any, formDirective: FormGroupDirective): void {     formDirective.resetForm();     this.myForm.reset(); } 

GitHub issue: https://github.com/angular/material2/issues/4190

like image 143
Harry Ninh Avatar answered Sep 27 '22 02:09

Harry Ninh


After reading the comments this is the correct approach

// you can put this method in a module and reuse it as needed resetForm(form: FormGroup) {      form.reset();      Object.keys(form.controls).forEach(key => {       form.get(key).setErrors(null) ;     }); } 

There was no need to call form.clearValidators()

like image 39
Savio Rodrigues Avatar answered Sep 24 '22 02:09

Savio Rodrigues