【问题标题】:Custom validator formArray controls自定义验证器 formArray 控件
【发布时间】:2019-02-18 21:56:03
【问题描述】:

我有一个formGroup,这个formGroup的控件是formArrays,

我的计划是遍历 formGroup.formArrays 的值并检查是否有任何值设置为 true,如果是,一切正常。

但是正在发生的事情是;它总是无效的

 static validate (control: AbstractControl): { [key: string]: boolean } | null  {
    let isChecked = false;
    const tempArray = [];
      for ( let i = 0; i < control.value.length; i++ ) {
      const val = control.value[i];
      tempArray.push(val);
      }
   isChecked = tempArray.find( el => el === true );
   if ( isChecked ) {
    return null;
    }
    return  {
      'isNotChecked' : true
    };
  }

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    因为您的退货方式错误.. 即如果它无效返回null。

    如果您在关注文档中的英雄。有双重否定..

    ** A hero's name can't match the given regular expression */
    export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
      return (control: AbstractControl): {[key: string]: any} | null => {
        const forbidden = nameRe.test(control.value);
        return forbidden ? {'forbiddenName': {value: control.value}} : null;
      };
    }
    

    禁止 - 因为它无法匹配正则表达式。

    (正则表达式测试将返回真匹配)

    这是我的一个例子:

    export function min(min: Number): ValidatorFn {
        return (control: AbstractControl): {[key: string]: any} => {
          const input = control.value,
          isValid = input < min;
          if(isValid) 
              return { 'minValue': {min} }
          else 
              return null;
        };
    }
    

    如果这有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      相关资源
      最近更新 更多