I have an Angular component that defines an FormGroup
, which has a nested FormGroup
as one of its controls.
The child FormGroup
is passed as an @Input
parameter to a child component, and there are validators on some of the controls inside the child FormGroup
.
For some reason, the valid
property of the parent FormGroup
only updates once I update values in the child component, then make a random change to one of the inputs for the parent FormGroup
(like adding an extra space to an input).
The setup is fairly complex (validators are added or removed from controls on the child FormGroup depending on certain conditions, which is probably why the validation isn't happening automatically).
How can I manually trigger the parent FormGroup
to re-validate as soon as anything in the child FormGroup
changes? I tried this in the child component:
ngOnInit()
this.myForm.valueChanges.subscribe(val => {
this.myForm.updateValueAndValidity({onlySelf: false, emitEvent: true})
});
This is supposed to trigger a re-validation of the child
FormGroup whenever any of its inputs change, and broadcast the event to the parent
component, which should trigger a re-validation of the parent FormGroup
. I get some kind of stack overflow error though, as this results in an infinite loop.
How can I trigger the parent FormGroup
to re-validate automatically?
this.FormGroup.updateValueAndValidity();
updateValueAndValidity() - this is a default method withing FormGroup