rock11
rock11

Reputation: 758

Trying to reset form on button click in angular

I am creating a basic sample user registration application with basic credentials.I am trying to reset the form when I click on the cancel button.However I am unable to do so. I've written the reset function but somehow it's not being called.I can't figure out what's wrong.Please help.Thanks in advance

My ts code:

 ngOnInit()
  {

    this.addForm = this.formBuilder.group({
      id: [],
      userName: ['', Validators.required],
      password:new FormControl( '',[ Validators.required]),
      passwordAgain: new FormControl('',[ Validators.required]),
      userRole:['',Validators.required],

    },{ validator: RepeatPasswordValidator });
  }
  onSubmit() {
    if (this.addForm.valid)
    {
    this.userService.createUser(this.addForm.value)
      .subscribe( data => {

        //this.router.navigate(['adduser']);
      });
      this.snackBar.open("message", "action", {
        duration: 2000,
      });
      alert("User created");

  }

  window.location.reload();
}
  changeClient(value) {

}
resetForm()
{
  this.addForm.reset();
}

Here is my template:

<div  style="height: 100vh" fxLayout="column" fxLayoutAlign="center center" >
  <mat-toolbar>
    <span>Registration</span>
  </mat-toolbar>
    <mat-card>
        <mat-card-content>

    <form [formGroup]="addForm" class="login-form" (ngSubmit)="onSubmit()">
    <div class="form-group">
        <mat-form-field class="example-full-width">

      <input matInput type="text" formControlName="userName" placeholder="userName" name="userName" class="form-control" id="userName">
        </mat-form-field>
    </div>

    <div class="form-group">
        <mat-form-field class="example-full-width">

      <input matInput type="password" formControlName="password" placeholder="Password" name="password" class="form-control" id="password">
      <mat-error *ngIf="addForm.controls.password.hasError('required')" >Passwords can't be empty</mat-error>
    </mat-form-field>
    </div>
    <div class="form-field">
        <mat-form-field>
        <input matInput formControlName="passwordAgain" placeholder="Confirm the password" type="password" [errorStateMatcher]="passwordsMatcher">
        <mat-error *ngIf="addForm.hasError('passwordsNotEqual')" >Passwords are different. They should be equal!</mat-error>
        </mat-form-field>
    </div>
    <mat-form-field>
        <mat-select placeholder="Roles" formControlName="userRole" id="userRole" (selectionChange)="changeClient($event.value)" [(value)]="selected">
          <mat-option value="Admin">Admin</mat-option>


        </mat-select>
      </mat-form-field>

      <div class="container" style="margin: 12px" fxLayout="row" fxLayoutAlign="center center">


    <button mat-raised-button  color="primary">Create</button>
    <button mat-raised-button (click)= 'resetForm()' color="primary">Cancel</button>


      </div>
  </form>


  </mat-card-content>
    </mat-card>
</div>

Upvotes: 3

Views: 11198

Answers (5)

nihals_dev
nihals_dev

Reputation: 11

No new method is necessary...
Just set the button type to reset as below,

<button mat-button color="primary" type="reset">Cancel</button>

This should work!!

Upvotes: 0

Mohi
Mohi

Reputation: 315

The button is a submit button (this is default for all browsers, except Internet Explorer) , please set the type to button

    <button mat-raised-button type="button" (click)= 'resetForm()' color="primary">Cancel</button>

Upvotes: 0

dileepkumar jami
dileepkumar jami

Reputation: 2265

It should be with parenthesis () inside the resetForm() method like below:

this.addForm.reset();

UPDATED ANSWER:

Also, try giving type=reset for the cancel button like below

<button mat-raised-button type=reset (click)= 'resetForm()' color="primary">Cancel</button>

Upvotes: 4

Mind R
Mind R

Reputation: 1

Might be fixed if you call .reset(); instead of .reset;

See here

if this is not working as u state try a console.log('called reset') in your function, if its called you can also use patchvalue() to test. (reset should work though)

edit: u might also try this from the docs:

console.log(this.addForm.value);  // {password: 'abcdef', passwordAgain: 'abcdef'}

this.addForm.reset({ password: '', passwordAgain: '' });

If this doesn't show, than i'd like to see how you defined your addForm

Upvotes: 0

Hackensack
Hackensack

Reputation: 27

Reset should be a function

resetForm()
{
  this.addForm.reset();
}

Upvotes: 0

Related Questions