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; } }


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*/


  • Check 'Manager can update membership list' in AD
  • create circular Auto Horizontal Scroll View?
  • android check if file exists with case sensitivity?
  • How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?
  • Populating a database table with returned JSON
  • Yii Bootstrap not loading JS files
  • How to put an object in the air?
  • Auto send email based on the time and email address in database
  • Firebase: Create/Update multiple child nodes atomically
  • How to get “crispEdges” for SVG text?
  • Pandas time series data Index from a string to float [duplicate]
  • Boolean filter using a timestamp value on a dataframe in Python
  • Checking for valid enum types from protobufs
  • LINQ to populate treeview based upon grouping
  • Compiling multiple source files in Rcpp
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • 'Edit' function for forum posts and such
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • how to specify different css for ie
  • When to use the tag in the head and body section of a html page? [duplicate]
  • VS2010 RDLC C#. How can I set a LocalReport object to a ReportViewer?
  • Calculate time from document
  • Conflicting declaration using constexpr and auto in C++11
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • DataTables move rows between tables
  • How to specify generic type when the type is only known at runtime?
  • Using redis as an LRU cache for postgres
  • How to change user identity when git pushing via ssh?
  • read part of h5 dataset python
  • Debug `Unexpected end of JSON input Error` on content script
  • Update cell query for Excel ADO from Delphi
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • How to call different template for different category archive page in woocommerce
  • customize soft keyboard key preview
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web