I try to implement a validator in Angular 11 but i get the compliation error:
Type '(controlArray: FormArray) => ValidationErrors | null' is not assignable to type 'ValidatorFn'. Any ideas how to fix this error?
Validator-class:
import { FormArray, ValidationErrors } from "@angular/forms";
export class BookValidators {
static atLeastOneAuthor(controlArray: FormArray): ValidationErrors | null {
if(controlArray.controls.some(el => el.value)){
return null;
}else{
return{
atLeastOneAuthor: {
valid: false
}
}
}
}
}
The calling class
constructor(private fb: FormBuilder){}
private buildAuthorsArray(values: string[]): FormArray{
// BookValidators.atLeastOneAuthor is now underlined with the error.
return this.fb.array(values, BookValidators.atLeastOneAuthor);
}