我正在从服务响应创建动态 FormGroup
和 FormArray
,现在我想验证表单数组是否有任何一个输入具有值,否则我没有想要验证。
this.billingForm = this._fb.group({
itemRows: this._fb.array([this.initItemRows()])
});
initItemRows() {
return this._fb.group({
Id: '',
orgName: '',
billing: this._fb.array([]),
payment: this._fb.array([])
});
}
最佳答案
你可以使用这样的东西:
// Subscribe to value changes on FormGroup
this._fb.valueChanges.subscribe(c => {
// Check empty values
if (this._fb.controls['Id'].value !== '' && this._fb.controls['orgName'].value !== '') {
// Set validators
this._fb.controls['billing'].validator = Validators.required; // You can specify any validator method here
this._fb.controls['payment'].validator = Validators.required;
} else {
this._fb.controls['billing'].validator = null;
this._fb.controls['payment'].validator = null;
}
});
您可以在此处获取有关自定义验证器的更多信息:Click!
关于angular - 动态表单数组验证 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47795339/