I have an Angular 7 project for dynamically creating forms. I have one parent FormGroup with nested FormGroups of various types.
I want parentForm to be invalid until all of the nested/subforms are valid (actually want them submitted but haven't gotten there yet).
this.parentForm = new FormGroup(this.subforms, { validators: allSubModulesValidValidator });
this.subforms is an object like this:
interface DynamicKeyFormGroup {
[key: string]: FormGroup;
}
subforms: DynamicKeyFormGroup = {};
I know my validator is wrong, but I can't figure out how to design a validator for a FormGroup vs a FormControl.
The idea is that I'm trying to loop over all of this.subForms' properties which are the nested FormGroups and then checking their status. If any are invalid, mark parentForm as invalid.
const allSubModulesValidValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null => {
const controls = control.controls;
for (const key in controls) {
if (controls.hasOwnProperty(key)) {
if (!(controls[key].status === 'valid')) {
return { 'allSubModulesValid': true };
}
}
}
return null;
};
In response to comment. After removing the validator the parent is valid while child is invalid: