I am using linear mat-stepper.
It works fine with next. I do api call & if it's success then i call stepper-next event instead of using matStepperNext directive.
Now when user fills all data on step 1 & directly click on some other step in header of next(or any other on edit record mode) then it will redirect to next step since form is valid.
I want to do service call before that & prevent step-change until service call is successful then only it should redirect to next step otherwise it shouldn't.
I can't find any method to implement this functionality in doc.
Can anyone suggest me what to do. I don't want to disable header click.
The following approach works for me:
<mat-vertical-stepper [linear]="true" #stepper>
....
</mat-vertical-stepper>
<mat-step [editable]="false" [optional]="false" [stepControl]="desiredServiceFormGroup">
<form [formGroup]="desiredServiceFormGroup">
...
</form>
</mat-step>
ngOnInit() {
this.desiredServiceFormGroup = this.formBuilder.group({
desiredTarget: [ '', Validators.required],
paymentAgreed: ['', Validators.required],
...
x: ['', Validators.required]
});
}
With this extra validator, your stepControl will always be false. When stepControl is false, step is not optional, not editable and stepper is linear. Direct clicks on step headers do not change the current step.
<button [disabled]="desiredTarget == null || !paymentAgreed" (click)="createVerification(desiredTarget.targetId, stepper)">NEXT</button>
async createVerification(targetId: number, stepper?: MatStepper) {
this.verification = await this.dataService.createVerification(targetId);
if (stepper !== undefined) {
this.desiredServiceFormGroup.removeControl('x');
stepper.next();
}
}
reset(stepper?: MatStepper) {
this.desiredServiceFormGroup.addControl('x', new FormControl('', [Validators.required]));
stepper.reset();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With