I would like to implement a mat-stepper, every step is in a different component, my problem is to prevent the next button is clicked if the step is not completed:
parent.html:
<mat-horizontal-stepper>
<mat-step [stepControl]="stepOneForm [completed]="stepOneForm?.valid">
<ng-template matStepLabel>Step1</ng-template>
<child-step-1
[stepOne]="stepOneForm"
(stepOneEmitter)="updateStepOne($event)">
</child-step-1 >
</mat-step>
<mat-step [stepControl]="stepTwoForm [completed]="stepTwoForm?.valid">
<ng-template matStepLabel>Step2/ng-template>
<child-step-2
[stepTwo]="stepTwoForm"
(stepTwoEmitter)="updateStepTwo($event)">
</child-step-2 >
</mat-step>
</mat-horizontal-stepper>
parent.ts:
stepOneForm: FormGroup;
stepTwoForm: FormGroup;
//////////
updateStepOne(step: FormGroup) {
this.stepOneForm = step;
}
updateStepTwo(step: FormGroup) {
this.stepTwoForm = step;
}
child-1.html
<form [formGroup]="stepOne" (change)="updateStepOne()">
<mat-form-field>
<input matInput required type="text" formControlName="title"/>
</mat-form-field>
<button mat-button matStepperNext><mat-icon>arrow_forward</mat-icon></button>
</form>
child-1.ts
this.stepOne = this.fb.group({
title: ['Default title', [
Validators.required
]]
});
updateStepOne() {
this.stepOneEmitter.emit(this.stepOne);
}
Everything works but the stepControl, it is possible to go to next step even when the form is not valid.
Where is my mistake?