以反应形式使用自定义验证器

时间:2017-06-14 13:24:28

标签: angular validation angular-reactive-forms

我创建了自定义验证指令:

import {Directive} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn} from "@angular/forms";


@Directive({
  selector: '[validator]',
  providers: [{provide: NG_VALIDATORS, useExisting: CIFValidator, multi: true}]
})
export class CIFValidator implements Validator {
  constructor() {}

  public validate(c: AbstractControl): ValidationErrors | any {
    return null
  }
}

我目前使用它作为属性,但如果我想在反应式配置中使用它,我应该怎么做,如下所示:

this.form = this.formBuilder.group({
    someField: ['', [Validators.NewValidator]]
});

0 个答案:

没有答案