Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 reactive form is not clearing the validations after resetting the form

Tags:

angular

I am using Angular 4.4.6 reactive form with Angular Material 2.0.0-beta.12 in my application. This is my component,

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
    selector: 'app-quick-file',
    templateUrl: './quick-file.component.html',
    styleUrls: ['./quick-file.component.css']
})
export class QuickFileComponent implements OnInit {

    quickFileForm: FormGroup;


    constructor() { }

    ngOnInit() {
        this.initQuickFileForm();
    }

    private initQuickFileForm () {
        this.quickFileForm = new FormGroup({
            'fileOpenDate': new FormControl(new Date(), Validators.required),
            'fileSubjectEn': new FormControl(null, Validators.required),
            'categoryId': new FormControl(null, Validators.required),
            'subCategoryId': new FormControl(null),
            'primaryClientId': new FormControl(null, Validators.required),
            'primaryConsultantId': new FormControl(null, Validators.required)
        });
    }

    saveQuickFileForm() {
        console.log(this.quickFileForm);
        this.quickFileForm.reset();
    }

}

Here is my template,

<div>
    <form [formGroup]="quickFileForm" (ngSubmit)="saveQuickFileForm()">
        <mat-form-field class="common-form-field">
            <input matInput [matDatepicker]="openDatePicker" formControlName="fileOpenDate" placeholder="Choose Date">
            <mat-datepicker-toggle matSuffix [for]="openDatePicker"></mat-datepicker-toggle>
            <mat-datepicker #openDatePicker></mat-datepicker>
        </mat-form-field>

        <mat-form-field class="common-form-field">
            <input type="text" matInput formControlName="fileSubjectEn" placeholder="Subject in English">
        </mat-form-field>

        <mat-form-field class="common-form-field">
            <mat-select formControlName="categoryId" placeholder="Choose category">
                <mat-option value="1">opt 1</mat-option>
                <mat-option value="2">opt 2</mat-option>
                <mat-option value="3">opt 3</mat-option>
            </mat-select>
        </mat-form-field>

        <mat-form-field class="common-form-field">
            <mat-select formControlName="subCategoryId" placeholder="Choose subcategory">
                <mat-option value="1">opt 1</mat-option>
                <mat-option value="2">opt 2</mat-option>
                <mat-option value="3">opt 3</mat-option>
            </mat-select>
        </mat-form-field>

        <mat-form-field class="common-form-field">
            <mat-select formControlName="primaryClientId" placeholder="Choose subcategory">
                <mat-option value="1">opt 1</mat-option>
                <mat-option value="2">opt 2</mat-option>
                <mat-option value="3">opt 3</mat-option>
            </mat-select>
        </mat-form-field>

        <mat-form-field class="common-form-field">
            <mat-select formControlName="primaryConsultantId" placeholder="Choose subcategory">
                <mat-option value="1">opt 1</mat-option>
                <mat-option value="2">opt 2</mat-option>
                <mat-option value="3">opt 3</mat-option>
            </mat-select>
        </mat-form-field>
    </form>
</div>

Validation errors are showing even after I reset the form. I also tried the following methods.

this.quickFileForm.clearValidators();
this.quickFileForm.markAsPristine();
this.quickFileForm.markAsPristine();

What could be the error in my code and the possible solution? Thank you.

like image 421
shafeequemat Avatar asked Nov 02 '17 03:11

shafeequemat


2 Answers

@AJT_82, we've modified a bit of your code dude.

<form [formGroup]="quickFileForm" (ngSubmit)="saveQuickFileForm(f)" #f="ngForm">

saveQuickFileForm(form) {
  console.log(this.quickFileForm);
  form.resetForm();
}

Eliminated @ViewChild, Seems to work also..BTW Thanks for the help! :)

like image 144
Edwin Bermejo Avatar answered Oct 18 '22 18:10

Edwin Bermejo


This seems to be a known bug when having a button of type submit. There are some work-arounds presented in that issue, of which I would use the ngForm directive:

<form [formGroup]="quickFileForm" (ngSubmit)="saveQuickFileForm()" #f="ngForm">

TS:

@ViewChild('f') myForm;

saveQuickFileForm() {
  this.myForm.resetForm();
}

This seems to work fine! DEMO

like image 36
AT82 Avatar answered Oct 18 '22 18:10

AT82