I have a nested forms. I used the patchValue to initialize the child form. Many people here may ask why I didn't set child form fields with default value while creating a new form, the answer is the child form need to be initialized from a REST API, but in sample code, I just use patchValue to demo my issue.
ngOnInit() {
this.form = new FormGroup({
input: new FormControl("", [Validators.required, Validators.maxLength(5)])
});
this.form.patchValue({
input: "test"
});
this.form.valueChanges
.pipe(untilDestroyed(this))
.subscribe((value: string) => {
this.onChangeFunc(value);
});
}
Please refer to my sample code: https://stackblitz.com/edit/angular-8-app-example-ktwtye
Problem: the child form value are initialized, (state is valid), but the parent form value is still null (state is invalid)
Any suggestion will be appreciated about : How to properly update parent form value after child form value is updated by patchValue or setValue ?