I have a data driven form in angular2 like below
this.formBuilder.group({
\'name\': [\'\',Validators.required],
\'description\': [\'\', Validators.required],
Add this custom validator to your validation service:
minLengthArray(min: number) {
return (c: AbstractControl): {[key: string]: any} => {
if (c.value.length >= min)
return null;
return { 'minLengthArray': {valid: false }};
}
}
And then when creating the form do the following:
this.formBuilder.group({
'name': ['',Validators.required],
'description': ['', Validators.required],
'places': this.formBuilder.array([], this.validationService.minLengthArray(1))
});
And you can check errors against the FormArray
by checking FormArray.hasError('minLengthArray')