You has considerhave considered if your FormArray is a FormArray of FormControls or a FormArray of FormGroups, but the problem is how you iterate over the controls,
A simple example of both
export function customValidateArray(): ValidatorFn {
return (formArray:FormArray):{[key: string]: any} | null=>{
let valid:boolean=true;
formArray.controls.forEach((x:FormControl)=>{
valid=valid && x.value=="a"
})
return valid?null:{error:'Not all a'}
}
};
export function customValidateArrayGroup(): ValidatorFn {
return (formArray:FormArray):{[key: string]: any} | null=>{
let valid:boolean=true;
formArray.controls.forEach((x:FormGroup)=>{
valid=valid && x.value.name=="a"
})
return valid?null:{error:'Not all name are a'}
}
};
export function customValidateArray(): ValidatorFn {
return (formArray:FormArray):{[key: string]: any} | null=>{
let valid:boolean=true;
formArray.controls.forEach((x:FormControl)=>{
valid=valid && x.value=="a"
})
return valid?null:{error:'Not all a'}
}
};
export function customValidateArrayGroup(): ValidatorFn {
return (formArray:FormArray):{[key: string]: any} | null=>{
let valid:boolean=true;
formArray.controls.forEach((x:FormGroup)=>{
valid=valid && x.value.name=="a"
})
return valid?null:{error:'Not all name are a'}
}
};
You can see an example in stackblitz
NOTE: To create the form, I use the constructor of FormGroup, FormControl and FormArray, but you can use FormBuilder too.
NOTE 2: it's not necesary enclose a FormArray in a FormGroup