3

I'm trying to reset my form after it is sent, but only the value is set to null.

component.html

  <div *ngIf="!loading" fxLayout="row" class="note-textarea">
    <form  fxFlex fxLayout="column" fxLayoutGap="10px" [formGroup]="noteForm">
      <mat-form-field fxFlex>
        <textarea matInput #note rows="1" maxlength="100" placeholder="Note" formControlName="description"></textarea>
        <mat-hint align="end">{{note.value?.length || 0}}/100</mat-hint>
        <mat-error *ngIf="noteForm.get('description').errors && noteForm.get('description').touched">description is required</mat-error>
      </mat-form-field>
      <div fxFlex>
        <button mat-stroked-button class="save-btn" (click)="insertNote()">Save</button>
      </div>
    </form>
  </div>

component.ts

  noteForm: FormGroup = this.formBuilder.group({
    description: new FormControl(null, Validators.required)
  })
 insertNote() {
   // bunch of code 

      this.noteForm.reset();

    }
  }

the problem is that input field is mark as dirty as you can see below:

enter image description here

3
  • Seems like a duplicate of the question: stackoverflow.com/questions/34608361/… Commented Feb 4, 2020 at 9:54
  • @Ivilin Stouanov, Read about using resetForm or type=reset. Commented Feb 4, 2020 at 10:10
  • reset will resets the FormGroup by marks all descendants are marked pristine and untouched, and the value of all descendants to null. 🤔🤔 Commented Feb 4, 2020 at 12:13

4 Answers 4

8

Try with button type="reset" option

<button type="reset">Reset</button> 

Stackblitz Example

Sign up to request clarification or add additional context in comments.

1 Comment

this wil reset the from but he try reset after click save and run insertNote method from the component body
2

You can use ngForm to do so

In your Html file

<form  fxFlex fxLayout="column" fxLayoutGap="10px" [formGroup]="noteForm" #noteForm="ngForm">

In your ts file

 @ViewChild('noteForm', { static: true }) noteForm: NgForm;
//to reset form
this.noteForm.resetForm();

Replace name accordingly in your HTML and TS files.

Comments

0

you can use formGroup reset method like this (click)="noteForm.reset()" and this method will marks all descendants are marked pristine and untouched, and the value of all descendants to null.

example

<div [formGroup]="form">
    <input placeholder="description..." type="text" formControlName="description" ><br/><br/>

    <div
        *ngIf="form.get('description').hasError('required') && (form.get('description').dirty || form.get('description').touched) ">

        description is required

    </div>
    <button type="button" (click)="insertNote()">Add 💾</button> &nbsp;
    <button type="button" (click)="form.reset()">reset 🧹</button>

</div>

demo 🚀

with angular material you don't need to check if the controls is touched

   <mat-error *ngIf="noteForm.get('description').hasError('required')">
       description is required
   </mat-error>

demo 🚀

I don't use this <button type="reset">Reset</button> because most the time I want to reset the form from the component and sometimes I want to pass initial value after reset

Comments

-1
for (let control in this.form.controls) {
  this.form.controls[control].setErrors(null);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.