Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Forms - skip validation on Cancel

I have a reactive form which has Cancel and Submit buttons:

<button (click)="cancel($event)" type="button" class="ui button">Cancel</button>
<button [disabled]="..." type="submit" class="ui button primary">Store</button>

and now if I click on a Submit (Store) button validation kicks in - all good. But if I click on Cancel it also trigger validation. I wonder why? I don't need any validation on Cancel. What do I need to do to turn it off?

like image 834
alvipeo Avatar asked Sep 16 '17 01:09

alvipeo


2 Answers

You can simply 'reset' the formControls in the parent formGroupon cancel button click if that is okay for you.

cancel() {
 this.form.reset();
}
like image 107
amal Avatar answered Nov 14 '22 23:11

amal


It works with event.preventDefault()

<button (click)="cancel($event)">Cancel</button>
cancel(event) {
    event.preventDefault()
}
like image 38
Boris Avatar answered Nov 15 '22 01:11

Boris