I have a FormArray with simple inputs. I want to pass validator function (custom) an index of the input in FormArray, since my validator is based on that. For example, I want each subsequent AbstractControl to have more letters than the one before (this is only just an example, passing an index is what I really need to achieve).
I mean if I can get something like this
ngOnInit(): void {
this.items = this.items?.length ? this.items : this.getDefaults(this.size)
const inputs: FormArray = this.formGroup.get('inputs') as FormArray
for (let item of this.items) {
inputs.push(this.fb.control(
item.value,
this.validator(),
this.asyncValidator(),
))
}
}
private validator(): ValidatorFn {
return (abstractControl: AbstractControl/*, indexInArray */) => {
// const lenOfPrev = this.items[indexInArray - 1].value.length
if (abstractControl.value.length <= 0 /* lenOfPrev */) {
return {
notLargeEnough: 'should be bigger'
}
}
return null
}
}