I am trying to implement Form Array in custom multi-step form using Angular-12.
Component:
export class ProfileEditComponent implements OnInit {
isLinear = true;
isLoading = false;
isSubmitted = false;
multistepForm!: FormGroup;
step: any = 1;
constructor(
private fb: FormBuilder
) {}
ngOnInit(): void {
this.buildForm();
}
onFormSubmit() {
this.step = this.step + 1;
}
previous() {
this.step = this.step - 1;
}
buildForm() {
this.multistepForm = this.fb.group({
// multistepDetails: new FormGroup({
personalDetails: new FormGroup({
first_name: new FormControl('', [Validators.required, Validators.maxLength(50)]),
last_name: new FormControl('', [Validators.required, Validators.maxLength(50)]),
other_name: new FormControl('', [Validators.maxLength(50)]),
}),
educations: this.fb.array([
this.createEducation()
])
});
}
createEducation() {
return this.fb.group({
educationDetails: new FormGroup({
city: new FormControl('', Validators.minLength(3)),
country: new FormControl('', Validators.minLength(3))
}),
})
}
}
See Question&Answers more detail:os