Validator in not a function in angular

Discussão em 'Angular' iniciado por Mani Nalliappan, Outubro 24, 2024.

  1. I have created own directive for adding validation and removing validation based input parameters of directive. I am getting error when loading and form field value changing validator is not working the validation

    @Directive({
    selector:[appdirective1],
    providers:[{
    provide:NG_VAILDATORS,
    useExisting:forwardRef(()=>directiveName)

    }]
    });

    export class directiveName implements OnChanges,OnInit{
    @Input varName1="";
    @Input varName2="";
    @Input callBack1:()=>{}
    @Input callBack2:()=>{}
    constructor(private _injector:Injector){
    }

    validate1():ValidateFn{
    return (control:AbstractControl):{[key:string]:any}|null =>{
    //logic here
    }
    }
    validate2():ValidateFn{
    return (control:AbstractControl):{[key:string]:any}|null =>{
    const obj1=this.callBack1();
    //logic here
    }
    }
    validate3():ValidateFn{
    return (control:AbstractControl):{[key:string]:any}|null =>{
    const obj1=this.callBack2();
    //logic here
    }
    }
    ngOnInit(){
    const control=this._injector.get(NgControl);
    control.control.setValidatoers(this.validate1);
    if(this.varName1 !=="" && this.varName1 !== undefined){
    control.control.setValidatoers(this.validate2);
    }
    if(this.varName2 !=="" && this.varName2 !== undefined){
    control.control.setValidatoers(this.validate3);
    }
    control.control.updateValueAndValidity();


    }
    ngOnChanges(){
    const control=this._injector.get(NgControl);
    if(this.varName1 !=="" && this.varName1 !== undefined){
    control.control.setValidatoers(this.validate2);
    }
    if(this.varName2 !=="" && this.varName2 !== undefined){
    control.control.setValidatoers(this.validate3);
    }
    control.control.updateValueAndValidity();
    }
    }
    }


    In the html file , I have added directive selectors and input paramets

    <input appdirective1 name="name2" [varName1]="success" [callBack1]="method1" />

    <input appdirective1 name="name3" [varName2]="success" [callBack2]="method2"/>


    Already method method1 and method2 binded in component file. I need to used this file many places.

    I am getting Error like :

    TypeError : validator is not a function
    at Forms.mjs
    at Array.map(<anonymous>)
    at executeValidators(forms.mjs:896:21)
    at Forms.mjs
    at Array.map(<anonymous>)
    at executeValidators(forms.mjs:896:21)
    at Formcontrol2._ComposedValidatorsFn(forms.mjs)
    at Formcontrol2._runValidator(forms.mjs)
    at Formcontrol2.updateValueandValidity(forms.mjs)

    Continue reading...

Compartilhe esta Página