Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
angular 2 pass array to custom validator (Template driven form)

I need to pass an array of objects to the a2 custom validator and then I d like to validate the value of the template driven form field against the records in that array.

However I can not retrieve the object inside the validator.

The only thing I can see is its name as string.

Any help is kindly appreciated.

<label class="btn btn-default btn-sm"  
[(ngModel)]="krediHesaplamaModel.radioModelKrediTur" name="krediHesaplamaModel.radioModelKrediTur" 
btnRadio="0"  
(click)="onRadioButtonKrediHesaplamaTurChange()" krediTuruValidator="this.krediList" >

import { Directive, forwardRef, Attribute } from '@angular/core';
import { Validator, AbstractControl, NG_VALIDATORS } from '@angular/forms';
import {Kredi} from '../kredi';

@Directive({
    selector: '[krediTuruValidator][formControlName],[krediTuruValidator][formControl],[krediTuruValidator][ngModel]',
    providers: [{ provide: NG_VALIDATORS, useExisting: forwardRef(() => KrediTuruValidator), multi: true },
    ]

})


export class KrediTuruValidator implements Validator {

    constructor(  public krediTuruValidator: Kredi[]) { }

    validate(c: AbstractControl): { [key: string]: any } {
        console.log('KL' + this.krediTuruValidator[0].krediTuru); //UNDEFINED

        let v = c.value;
        return null;
    }

}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I resolved this issue by delegating the validation function to another method in the component. That way I can access any object I desire

import { Directive, forwardRef, Attribute, Input } from '@angular/core';
import { Validator, ValidatorFn, AbstractControl, NG_VALIDATORS } from '@angular/forms';


@Directive({
    selector: '[krediVadeSayisiValidator][formControlName],[krediVadeSayisiValidator][formControl],[krediVadeSayisiValidator][ngModel]',
    providers: [{ provide: NG_VALIDATORS, useExisting: forwardRef(() => KrediVadeSayisiValidator), multi: true },]

})


export class KrediVadeSayisiValidator implements Validator {

    @Input() krediVadeSayisiValidator: ValidatorFn; //same name as the selector

    validate(c: AbstractControl): { [key: string]: any } {

              return this.krediVadeSayisiValidator(c);
    }

}

How do I access it inside the template?

    <input type="number" class="form-control" name="krediVadeSayisi" [(ngModel)]="krediHesaplamaModel.krediVadeSayisi" #krediVadeSayisi="ngModel"
        required maxlength="2" [krediVadeSayisiValidator]="validateKrediVadeSayisi()" />  /*this function is inside the component*/
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda