Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - Calling Child Validators on Angular MatStepper Next in Parent Component

I Want To Validate My Mat-Stepper-Next Function in App Component Through Child Component Validators,

I've 3 Different Forms Components & Every Component Has There Own FormGroup, Validations & Next Button in The Component

I'm Calling That Child Components in My Parent Component & My Mat-Stepper is Present in The Parent Component

So Help Me That How To Call The Validations of Child Components in The MatStepper Next Function? Child Components Has Next Button in It

Like If Form is In-Complete or Has Errors Then It Won't Let Us To Jump into Next Form

Parent Component

----------------------------------------------
<mat-horizontal-stepper>
        <mat-step label="Form-1">
            <app-form1></app-form1>
        </mat-step>
        <mat-step label="Form-2">
            <app-form2></app-form2>
        </mat-step>
        <mat-step label="Form-3">
            <app-form3></app-form3>
        </mat-step>
</mat-horizontal-stepper>
question from:https://stackoverflow.com/questions/65557870/calling-child-validators-on-angular-matstepper-next-in-parent-component

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the completed property of mat stepper to restrict the next button if any of the field is invalid.

<mat-horizontal-stepper [linear]="true">
        <mat-step label="Form-1" [completed]="appForm1?.isCompleted ? true : false">
            <app-form1></app-form1>
        </mat-step>
        <mat-step label="Form-2" [completed]="appForm2?.isCompleted ? true : false">
            <app-form2></app-form2>
        </mat-step>
        <mat-step label="Form-3" [completed]="appForm3?.isCompleted ? true : false">
            <app-form3></app-form3>
        </mat-step>
</mat-horizontal-stepper>

In your parent.ts component you need to add child component references.

@ViewChild(AppForm1Component) public appForm1: AppForm1Component;
@ViewChild(AppForm2Component) public appForm2: AppForm2Component;
@ViewChild(AppForm3Component) public appForm3: AppForm3Component;

And in your each component you can add get property isCompleted which will return validation and other check whatever you want to add for NEXT button. Like in your AppForm1Component.ts:

get isCompleted(): boolean {
     return (this.form.valid && other checks)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...