There are multiple sibling component with different NgForm (template-driven forms) inside each of them. All of them are wrapped with a parent component. Parent component has validation button. I need to validate certain child NgForm within click event on the button on a parent component.
What approaches to solve this would you propose?
// Parent template
<child id="0"></child>
<child id="1"></child>
// Parent component
validateChild(id: number) {
// need somehow reach child component NgForm with a particular id
}
// Child Component template
<form #myForm="ngForm">...</form>
// Child Component template
@ViewChild('myForm')
public myForm: NgForm;
When I am saying validation - I mean referring to following:
Object.keys(form.controls).forEach(key => {
form.controls[key].markAsTouched();
})
I was thingking about following approach:
// Parent component
stages = [
{
validated: false
},
{
validated: false
},
]
// Parent component view
<child id="0" [validate]="stages[0].validated"></child>
<child id="1" [validate]="stages[1].validated"></child>
// Child component
@Input()
public set validate(value: boolean) {
if (value === true)
Object.keys(this.myForm.controls).forEach(key => {
this.myForm.controls[key].markAsTouched();
})
}
But I don't really like it..Probably there is a better approach. Because with this one without extra child EventEmitter I can't check whether form is valid or not