How can I implement my custom Validator in Angular 2?
I found this plunker:
constructor(private fb: FormBuilder) {
this.form = fb.group({
'singleSelection': ['Rio', [App.validateCity]] // initial value as string
'multipleSelection': [['Red','Blue'], [App.validateColor]] // initial value as array
});
}
static validateCity(c: FormControl) {
if (c.value !== 'New York') {
return { citySucks: true };
}
return null;
}
static validateColor(c: FormControl) {
if (c.value.indexOf('Green') < 0) {
return {badColor: true};
}
return null;
}
But, I think it would be better to implement an interface Validator, like class MinLengthValidator. But, I don't know how to use it!