0

I have a submit and a cancel button in my dialogbox, when i click on submit button the validatation works, but I want when i click on cancel button, form to be closed without need validation...Now when I click on cancel button validation fired and for second time form closed.

HTML code:

<button  mat-button (click)="closeDialog()" mat-flat-button>Cancel</button>

ts code:

  closeDialog(){
    this.dialogRef.close({event:'Cancel'});
  }

How I can close form with one click? Thank you...

kian
  • 1,449
  • 2
  • 13
  • 21
H Jafari
  • 19
  • 2
  • 8
  • It's hard to say without actual reproduction of your code on stackblitz. You're mentioning validation and submitting, so I assume you're using a `
    `. Note that by default, all buttons within a form will have a [type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type) of `submit`. If your close button is inside a form, try applying a `type="button"` to it.
    – TotallyNewb Oct 25 '21 at 08:27

1 Answers1

-1

you can use mat-dialog-close on your button tag, didn't need extra function

<button mat-button mat-dialog-close mat-flat-button>Cancel</button>

angular material mat-dialog examples

stackblitz example

kian
  • 1,449
  • 2
  • 13
  • 21
  • Thanks, I used your code, but I still need two clicks to close the dialog box. The second link you sent gives an error ... Thank you for checking – H Jafari Oct 25 '21 at 08:09
  • I fixed second link, please add more code to your question for better help.@HodaJa'fari – kian Oct 25 '21 at 08:16
  • Doesnt work. The formcontrol which is required still show red when dialog is closed. – chitgoks Aug 16 '23 at 04:07