Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset form validation on submission of the form in ANGULAR 2

I have to reset my form along with validation. is there any method to reset the state of form from ng-dirty to ng-pristine.

like image 364
Mubashir Avatar asked Jan 05 '16 09:01

Mubashir


People also ask

How do you reset the form after submit in Angular?

import { FormsModule } from '@angular/forms'; In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.

How would you reset all objects on a form in Angular?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.

How do you reset the template driven form?

To reset template-driven form, NgForm provides resetForm() method that is called as following. To call the above function, create a button in UI. If we want to reset form with some default values, then assign the default values with form control name of the form.


3 Answers

Here's how it currently works with Angular 4.1.0 - 5.1.3:

class YourComponent {
    @ViewChild("yourForm")
    yourForm: NgForm;


    onSubmit(): void {
        doYourThing();

        // yourForm.reset(), yourForm.resetForm() don't work, but this does:
        this.yourForm.form.markAsPristine();
        this.yourForm.form.markAsUntouched();
        this.yourForm.form.updateValueAndValidity();
    }
}
like image 132
Benny Bottema Avatar answered Oct 18 '22 06:10

Benny Bottema


from.resetForm()

I've tried pretty much everything, and the only thing I found that actually resets form.submitted to false is the following:

In your template, send your form into the submit function:

<form name="form" class="form-horizontal" (ngSubmit)="f.form.valid && submit(f)" #f="ngForm" novalidate>

In your component.ts file, have the following:

// import NgForm
import { NgForm } from '@angular/forms';

// get the passed in variable from the html file
submit(myForm: NgForm): void {

    console.log(myForm.form.status, myForm.form.dirty, myForm.form.pristine, myForm.form.untouched, myForm.submitted, myForm.dirty, myForm.pristine, myForm.untouched);

    // This is the key!
    myForm.resetForm();

    console.log(myForm.form.status, myForm.form.dirty, myForm.form.pristine, myForm.form.untouched, myForm.submitted, myForm.dirty, myForm.pristine, myForm.untouched);


}

The console.log values output the following - notice it resets all values.

VALID true false false true true false false

INVALID false true true false false true true

like image 20
cmartin Avatar answered Oct 18 '22 05:10

cmartin


app.component.html

The #formDirective="ngForm" and passing the formDirective to the onSubmit method is crucial for resetting error styles such as mat-error. Check #4190 to follow this bug and in-depth explanation why it's needed and how it works under the hood.

<form [formGroup]="contactForm" (ngSubmit)="onSubmit(contactForm.value, formDirective)" #formDirective="ngForm"> 
  <!-- Your input elements... -->
  <button [disabled]="!contactForm.valid">Submit</button>
</form>

app.component.ts

Remember about the FormGroupDirective which you need to import from @angular/forms (Angular/Material 9). To make the form empty call this.contactForm.reset(); and the form will be invalid, it's fine. However, you also need to reset the undesired validators styles, and this is a different method, i.e. formDirective.resetForm();.

Notice difference between formDirective and formData with its different built-in methods.

import { FormGroupDirective } from '@angular/forms';

public contactForm: FormGroup = this.formBuilder.group({
  // Your input elements...
});

public onSubmit(
  formData: FormGroup,
  formDirective: FormGroupDirective
): void {
  this.contactForm.reset(); // Reset form data
  formDirective.resetForm(); // Reset the ugly validators
}
like image 8
Daniel Danielecki Avatar answered Oct 18 '22 07:10

Daniel Danielecki