I'm trying to use Angular Material with formGroup in Angular 2 and I have a issue with input validation for nested formControls in differents components.
My problem is: when submitting a form, only the input in the first formGroup get notified that the form has been submitted.
I have created the following exemple:
@Component({
selector: 'material-app',
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {
public myForm: FormGroup;
constructor(private _fb: FormBuilder) {}
ngOnInit() {
this.myForm = this._fb.group({
nested: this._fb.group({
id: ['', Validators.required]
}),
id: ['', Validators.required],
});
}
}
I have a simple formGroup with one nested formController. This is my HTML:
<form [formGroup]="myForm">
<md-input-container>
<input mdInput required formControlName="id">
</md-input-container>
<other-component [myFormGroup]="myForm" myFormGroupName="nested"></other-component>
<button md-raised-button type="submit">Rechercher</button>
</form>
The other component just display another input.
I made a plunker to illustrate: http://plnkr.co/edit/WR0cmVOhIfCdkqAVc8xX
You can notice that if I enter a field and quit it right away, the red error line appears on both input. But, if I touch none of the two input and I click on submit, only the non-nested input get underlined. This is because the nested one don't get the information that the form was submitted, even if I pass the formGroup object as a parameter.
Any idea of how can I resolve this problem? How can I make the first input aware of the submitted form?
Thank you a lot !