9

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.

5
  • show your error log or it impossible to help you. Commented Nov 2, 2017 at 3:31
  • @RachChen Actually there is no error showing. Form values are clearing. But red color of the material input field (validation error) is not going. Commented Nov 2, 2017 at 3:39
  • Could you show your Demo on plunk? Commented Nov 2, 2017 at 3:51
  • Now formGroup.reset() is working without any bug in Angular 5. I have updated my Angular project to 5 and now it's working, no need to use localReference and resetForm() method. Commented May 16, 2018 at 3:19
  • resetForm() below may lead to unexpected behavior. You can define your own validations methods - see example here - stackoverflow.com/a/52676157/6332774 Commented Oct 6, 2018 at 6:11

3 Answers 3

18

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

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

3 Comments

You are very welcome :) Have a nice day and happy coding! :)
Hi, i have same problem with reset..but this does not seem to work for me :(
Sorry my bad..it works dude thanks for this! just forget to empty cache on refresh..:)
2

@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! :)

Comments

0

Just in case nothing visually works for you, I did it via jquery:

$("#form .form-control").each((idx, ctrl)=>{$(ctrl).removeClass("is-invalid")});

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.