Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent onSubmit() function getting called on click of a cancel button

I have one angular material dialog box and inside that there is one template driven form. It is working well when I click on submit button however when I click on cancel button then also onSubmit() function is getting called. how to prevent that? this is my code snippet

// ts file
export class RejectPopupComponent implements OnInit {
   submitted:boolean=false;
  comment: string;
  loanID: string;
  createdDate: string;
  constructor(
    private dataService: DataService,
    public dialogRef: MatDialogRef<RejectPopupComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {
    this.dataService.loanIdSource.subscribe(loanID => (this.loanID = loanID));
    console.log(this.loanID);

    this.dataService.applicationDateSource.subscribe(
      applicationDate => (this.createdDate = applicationDate)
    );
    console.log(this.loanID, this.createdDate);
  }
 
  onNoClick(): void {
    this.dialogRef.close();
  }

     onSubmit(form:any){
      console.log('hello')
    console.log(form);
       const statusDecisionInput = { loanID: "", createdDate: "", approverAction: "Rejected", reasonToReject: '' };
   statusDecisionInput.loanID = this.loanID;
   statusDecisionInput.createdDate = this.createdDate;
   statusDecisionInput.reasonToReject = form.reasonToReject;
   console.log(statusDecisionInput);
    this.submitted=true;
  }


  ngOnInit() {}
}
 <form #f="ngForm" (ngSubmit)="onSubmit(f.value)">
<div mat-dialog-content>
  <p>Why are you rejecting?</p>
    <textarea matInput rows="10" cols="38"  placeholder="Leave a comment" name="reasonToReject" ngModel required></textarea>
  </div>
<div mat-dialog-actions>
  <button mat-button (click)="onNoClick()">Cancel</button>
  <button mat-button class="submit-button" type='submit' 
  [disabled]="f.invalid" cdkFocusInitial (click)="onNoClick()">Submit</button> 
</div>
</form>
like image 875
Humble Dolt Avatar asked Jan 10 '18 07:01

Humble Dolt


People also ask

Why are all my buttons triggering onSubmit?

You may want to add a 'reset' or 'cancel' button to your form. When doing so, be wary of the type property passed to your <button/> . This can cause all buttons within your form to trigger onSubmit .

How do I stop form submit when onclick event return false?

Use the return value of the function to stop the execution of a form in JavaScript. False would return if the form fails to submit.

Which code snippet prevent the form from submitting if there is an validation error?

please use onsubmit attribute in the form element and write a javascript function to return false when there is any error.


1 Answers

Put type="button" explicitly to avoid button trigerring a form submission

<button mat-button type="button" (click)="onNoClick()">Cancel</button>

You can check the spec here Note that the default behaviour is submit unless specified otherwise.

like image 96
sabithpocker Avatar answered Oct 02 '22 22:10

sabithpocker