如何使用新的表单控件设置动态验证

问题描述 投票:0回答:0

我有一个对象应该构建一个表单

const textBox = {
  fontColor: 'red',
  fontSize: '20',
  placeholder: 'name',
  name: 'input name',
  label: 'mohamed',
  validation: {
    required: true
  }
};

我创建了一个简单的动态表单,但我需要为每个表单控件设置验证器

ngOnInit(): void {
  const inputDataObj = {} as any;
  for (const prop of Object.keys(this.InputObj)) {
    debugger;
    if (prop !== 'validation') {
      inputDataObj[prop] = new FormControl(this.InputObj[prop]);
      this.inputProps.push(prop);
    }
  }
  this.form = new FormGroup(inputDataObj);
}

现在我需要为任何表单控件设置验证,验证是一个可选属性, 有些对象可能没有任何验证

javascript angular typescript ecmascript-6 angular-reactive-forms
© www.soinside.com 2019 - 2024. All rights reserved.