Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MatHorizontalStepper stepControl with template driven forms

Is there any way to use [stepControl] error matcher with template driven forms? The docs just teach about an AbstractControl instance, which apparently forces the use of a reactiveForm.

I've tried to use [stepControl]="myNgForm" and [linear]="true" to validate the steps but the stepper just ignores it.

I appreciate any help.

Thanks!

like image 644
Alexandre Ducatti Avatar asked May 09 '18 21:05

Alexandre Ducatti


3 Answers

The step control seems to work with "form.control". Here an example with one form per step and template driven forms.

  <mat-vertical-stepper [linear]="true">
    <mat-step [stepControl]="form1.control">
       <form #form1="ngForm">
          <input [(ngModel)]="name" name="name" required />
       </form>
    </mat-step>
    <mat-step [stepControl]="form2.control">
       <form #form2="ngForm">
          <input [(ngModel)]="address" name="address" required />
       </form>
    </mat-step>
  </mat-vertical-stepper>
like image 151
Nicolas V Avatar answered Nov 15 '22 22:11

Nicolas V


use [stepControl]="myNgForm.controls.[controlGroup]"

<form #form="ngForm" novalidate>
  <mat-vertical-stepper [linear]="true">
    <mat-step label="Reporting person" ngModelGroup="reportor" [stepControl]="form.controls.reportor">
       <mat-form-field>
          <input matInput placeholder="First Name" name="firstName" ngModel required />
       </mat-form-field>
    </mat-step>
  </mat-vertical-stepper>
</form>
like image 30
Dhananjay kumar Avatar answered Nov 15 '22 23:11

Dhananjay kumar


The ngForm directive has a property form of type FormGroup

https://angular.io/api/forms/NgForm

like image 33
Paul Henry LEUFANG Avatar answered Nov 15 '22 22:11

Paul Henry LEUFANG