Javascript 在表格中向formArray添加动态表单字段和验证
我正在尝试构建一个动态表单,其中字段名及其验证将来自一个API 所以我创建了一个简单的表单,里面有一个formArray,类似这样:Javascript 在表格中向formArray添加动态表单字段和验证,javascript,angular,angular-reactive-forms,Javascript,Angular,Angular Reactive Forms,我正在尝试构建一个动态表单,其中字段名及其验证将来自一个API 所以我创建了一个简单的表单,里面有一个formArray,类似这样: this.myForm = this.formBuilder.group({ dynamicFields: this.formBuilder.array([]) // where I will be pushing the custom fields and their validations }); 为了进行测试,我尝试添加一个字段“country”,包
this.myForm = this.formBuilder.group({
dynamicFields: this.formBuilder.array([]) // where I will be pushing the custom fields and their validations
});
为了进行测试,我尝试添加一个字段“country”,包含2个验证(必需,minLength为22)
这就是我正在做的:
let customControl = this.fb.control('country', [ Validators.required, Validators.min(10) ]);
(this.myForm.get('dynamicFields') as FormArray).push(customControl);
我还创建了bellow getter,以便在模板中使用它
getDynFields() {
return this.myForm.get('dynamicFields') as FormArray;
}
这是模板:
<form [formGroup]="myForm">
<div formArrayName="dynamicFields">
<div *ngFor="let control of getDynFields().controls; let i = index">
<mat-form-field>
<input
[formControlName]="i"
matInput
type="text"
placeholder="{{ control.value }}"
/>
</mat-form-field>
</div>
</div>
</form>
正在发生的事情(如下所示)是,对于字段值和占位符,我将预先填充'country'(我只希望占位符为'country',值当然应该为空)
第二个问题是,只有所需的验证是有效的,minLength没有做任何事情
提前感谢。有关最小长度,请使用验证器。最小长度(22)
请参阅:了解更多信息。
最小值(10)用于添加验证,因为最小值可以是10。对于最小长度,请使用验证器。最小长度(22)
请参阅:了解更多信息。
用于添加验证的最小值(10)可以是10。FormControl在初始化时将
'state'
或默认值作为第一个参数。同样对于最小长度,您应该使用验证器.minLength(n)
而不是这个
let customControl = this.fb.control('country', [ Validators.required, Validators.min(10) ]);
你应该这样做
let customControl = this.fb.control('', [ Validators.required, Validators.minLength(10) ]);
FormControl在初始化时将
'state'
或默认值作为第一个参数。同样对于最小长度,您应该使用验证器.minLength(n)
而不是这个
let customControl = this.fb.control('country', [ Validators.required, Validators.min(10) ]);
你应该这样做
let customControl = this.fb.control('', [ Validators.required, Validators.minLength(10) ]);
Validators.min()与Validators.minLength()的比较
您正在使用。这是为了检查数字输入值,而不是获取字符串的长度:
let customControl = this.fb.control('country', [ Validators.required, Validators.min(10) ]);
改用:
FormControl的第一个参数是它的值
。您正在示例中设置“国家”值:
let customControl = this.fb.control('country', [ Validators.required, Validators.minLength(10) ]);
如果将其设置为null,您将看到已设置的任何占位符
let customControl = this.fb.control(null, [ Validators.required, Validators.minLength(10) ]);
您可以根据需要在模板中实现占位符。作为字符串或对某些对象属性的引用。Validators.min()vs Validators.minLength()
您正在使用。这是为了检查数字输入值,而不是获取字符串的长度:
let customControl = this.fb.control('country', [ Validators.required, Validators.min(10) ]);
改用:
FormControl的第一个参数是它的值
。您正在示例中设置“国家”值:
let customControl = this.fb.control('country', [ Validators.required, Validators.minLength(10) ]);
如果将其设置为null,您将看到已设置的任何占位符
let customControl = this.fb.control(null, [ Validators.required, Validators.minLength(10) ]);
您可以根据需要在模板中实现占位符。作为字符串或对某些对象属性的引用。如果您希望异步验证程序检查uniqness name或我推荐的其他内容,请访问此链接:-非常简单的示例。如果使用材料,请检查标签
。其次,您应该将min()
替换为minLength()
欢迎使用StackOverflow。旁注:请试着问一个问题。问两个问题会得到一半的答案。这甚至可能是结束你的问题的一个原因。如果你想让异步验证器检查uniqness名称或我推荐的东西,这个链接:-很好很简单的例子。如果使用材料,请检查标签
。其次,您应该将min()
替换为minLength()
欢迎使用StackOverflow。旁注:请试着问一个问题。问两个问题会得到一半的答案。这甚至可以成为结束你的问题的理由。