如何为角形反应形式的形式值添加自定义唯一验证器?

问题描述

我想添加一个自定义的唯一验证器,它将验证所有标签字段的值都是唯一的。 (I)当我更改表单值时,this.form的值会在 CustomValidator.uniqueValidator(this.form)中传递后更改。如何解决这个问题? (II)有没有不用任何包装的方法吗?

注意:表单在加载时具有默认值。这是屏幕截图。

enter image description here

this.form = this.fb.group({
      fields: this.fb.array([])
    });

private addFields(fieldControl?) {
return this.fb.group({
  label: [
    {value: fieldControl ? fieldControl.label : '',disabled: this.makeComponentReadOnly},[
    Validators.maxLength(30),CustomValidator.uniqueValidator(this.form)
    ]],isRequired: [
    {value: fieldControl ? fieldControl.isRequired : false,disabled: this.makeComponentReadOnly}],type: [fieldControl ? fieldControl.type : 'text']
});

}

  static uniqueValidator(form: any): ValidatorFn | null {
return (control: AbstractControl): ValidationErrors | null => {
  console.log('control..: ',control);
  const name = control.value;

  if (form.value.fields.filter(v => v.label.toLowerCase() === control.value.toLowerCase()).length > 1) {
    return {
      notUnique: control.value
    };
  } else {
    return null;
  }

}; }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)