Angular2 - 访问验证指令中的AbstractControl实例

时间:2017-01-26 18:16:13

标签: forms validation angular typescript angular2-directives

我必须从验证器指令的内部触发验证。

这是我的指令。它按预期工作。但是,我希望它在验证器函数更改时触发验证过程。即当其输入变量maxDate发生变化时。

我怎么能这样做?

如果我可以在构造函数中访问AbstractControl实例,我可以轻松地执行此操作。但是,我无法想出一种方法。

import { AbstractControl, FormGroup, ValidatorFn, Validator, NG_VALIDATORS, Validators } from '@angular/forms';
import { Directive, Input, OnChanges, SimpleChanges, EventEmitter } from '@angular/core';


function parseDate(date: string):any {  
  var pattern = /(\d{2})-(\d{2})-(\d{4})/;
  if (date) {
    var replaced = date.search(pattern) >= 0;    
    return replaced ? new Date(date.replace(pattern,'$3-$1-$2')) : null;
  }

  return date;
}


export function maxDateValidator(maxDateObj): ValidatorFn {
  return (control:AbstractControl): {[key: string]: any} => {    
    const val = control.value;

    let date = parseDate(val);
    let maxDate = parseDate(maxDateObj.max);    

    if (date && maxDate && date > maxDate) {
      return {
        maxDateExceeded: true
      };
    }
    return null;
  };
}

...

@Directive({
  selector: '[maxDate]',  
  providers: [{provide: NG_VALIDATORS, useExisting: maxDateDirective, multi: true}]  
})
export class maxDateDirective implements Validator, OnChanges {
  @Input() maxDate: string;

  private valFn = Validators.nullValidator;

  constructor() { }

  ngOnChanges(changes: SimpleChanges): void {
    const change = changes['maxDate'];
    if (change) {
      const val: string  = change.currentValue;      
      this.valFn = maxDateValidator(val);
    }
    else {
      this.valFn = Validators.nullValidator;
    }
    //This is where I want to trigger the validation again.
  }

  validate(control): {[key: string]: any} {
    return this.valFn(control);
  }
}

用法:

  <input type="text" [(ngModel)]="deathDateVal">

  <input class="form-control"          
          type="text"
          tabindex="1"
          [maxDate]="deathDateVal"
          name="will_date"
          [textMask]="{pipe: datePipe, mask: dateMask,  keepCharPositions: true}"
          ngModel
          #willDate="ngModel">

2 个答案:

答案 0 :(得分:3)

以下是我刚刚提出的内容:

@Directive({
  selector: '[maxDate]',  
  providers: [{provide: NG_VALIDATORS, useExisting: maxDateDirective, multi: true}]  
})
export class maxDateDirective implements Validator, OnChanges {
  @Input() maxDate: string;

  private valFn = Validators.nullValidator;
  private control:AbstractControl;

  constructor() { }

  ngOnChanges(changes: SimpleChanges): void {
    const change = changes['maxDate'];
    if (change) {
      const val: string  = change.currentValue;      
      this.valFn = maxDateValidator(val);
    }
    else {
      this.valFn = Validators.nullValidator;
    }

    if (this.control) {
      this.control.updateValueAndValidity(this.control);
    }
  }

  validate(_control:AbstractControl): {[key: string]: any} {
    this.control = _control;
    return this.valFn(_control);
  }
}

有效。初始化时会调用Validate,因此我只存储其参数。 这真是丑陋但它有效。

答案 1 :(得分:0)

要获得输入的abstractControl,可以执行以下操作:

project-prod-east-as