This is my form group. I'm using a form group inside another one:
this.shopGroup = this.fb.group({
_user: [''],
name: ['', Validators.compose([Validators.required, Validators.maxLength(60)])],
url_name: [''],
desc: ['', Validators.compose([Validators.required, Validators.maxLength(600)])],
photos: [''],
currency: ['Real'],
language: ['Português do Brasil'],
address: this.fb.group({
zipcode: ['', Validators.compose([Validators.required, Validators.pattern('[0-9]{5}[\-]?[0-9]{3}')])],
street: ['', Validators.compose([Validators.required, Validators.maxLength(70)])],
number: [null, Validators.compose([Validators.required, Validators.max(99999)])],
complement: ['', Validators.maxLength(30)],
district: ['', Validators.compose([Validators.required, Validators.maxLength(60)])],
state: ['', Validators.required],
city: ['', Validators.compose([Validators.required, Validators.maxLength(70)])]
}),
status: [true],
created_at: [new Date()],
updated_at: [new Date()]
});
Here is template:
<form [formGroup]="shopGroup">
<mat-horizontal-stepper [linear]="isLinear" #stepper>
// Im not sure how to set stepControl
<mat-step [stepControl]="?">
<ng-template matStepLabel>Store Creation</ng-template>
<mat-form-field>
<input matInput placeholder="Nome" formControlName="name">
</mat-form-field>
...
<div>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
// Im not sure how to set stepControl
<mat-step formGroupName="address" [stepControl]="?">
<ng-template matStepLabel>Configuração do Envio/Frete</ng-template>
<mat-form-field>
<input matInput placeholder="CEP" formControlName="zipcode">
</mat-form-field>
...
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
</div>
</mat-step>
</mat-horizontal-stepper>
</form>
This is the Angular Material
documentation for single forms:
<form [formGroup]="formGroup">
<mat-horizontal-stepper formArrayName="formArray" linear>
<mat-step formGroupName="0" [stepControl]="formArray.get([0])">
...
<div>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
<mat-step formGroupName="1" [stepControl]="formArray.get([1])">
...
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
...
</mat-horizontal-stepper>
</form>
I want to use shopGroup
on first step, then use address
(group inside shopGroup) on second step. Finally, i want to send shopGroup
. I'm aware that I need to set type="button"
between steps, and type="submit"
on the end, however i'm not sure how to set [stepControl]
to move from one step to another one. How to make it work on template (html)?
ERROR Error: Cannot find control with name: '0'
make sure you have setformArrayName
on the stepper<mat-horizontal-stepper formArrayName="formArray" ...>
. Took me some time to figure out. – Brewage