I've been creating reuseable subforms in Angular 8, and using the FormGroupDirective to get a reference of the parent FormGroup for the child is quite easy to accomplish, but I'm having a hard time figure out how to dynamically set the validators on the child form from the parent.
Using ngAfterViewInit I can see the form controls of the child forms have been added to the parent, but I can't seem to update the default child form validators directly from the parent.
// PARENT COMPONENT
public ngAfterViewInit() {
// Form controls have been applied to the form group
console.log('ngAfterViewInit', this.form.value);
// BUT the validators of profile form aren't being set?
this.form.get('profile').get('description').setValidators([Validators.required]);
}
I created a StackBlitz of my example code. All the fields except the profile description is a required field and has a default value. So without any changes being made to the validators the form will pass validations on submission and output VALID in the console. In ngAfterViewInit I set the description fields validator to be required, which should prevent the form from being submitted and output INVALID in the console, but it still submits and outputs VALID.
Is it possible to dynamically set the validators of a subform without passing them in via @Input bindings?