MatHorizontalStepper stepControl with template driven forms
Asked Answered
R

3

14

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!

Rapier answered 9/5, 2018 at 21:1 Comment(1)
Do you mean form input Validation?Lucielucien
P
17

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>
Panada answered 2/1, 2019 at 16:59 Comment(2)
Doesn't work for me in Angular 10: Property 'form1' does not exist on type 'MyComponent'Trakas
Should be marked as answer.Stratosphere
H
3

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>
Hatty answered 23/5, 2018 at 11:20 Comment(1)
Using stepControl and ngModelGroup necessarily switch to Reactive Form programming in Angular isn't it?Cella
K
-1

The ngForm directive has a property form of type FormGroup

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

Kipkipling answered 8/3, 2021 at 10:22 Comment(3)
can you provide context around the link in case it ever breaks? thank you!Grandeur
This can't be considered as an answer. Elaborate how this info relates to the question and how this info leads to a practical solution to the problem.Stratosphere
This does not help on the resolution of this problemHelbon

© 2022 - 2024 — McMap. All rights reserved.