【发布时间】:2020-03-30 21:57:36
【问题描述】:
无论如何要在 Angular Reactive Forms 中进行“要么,要么,一个或另一个验证”?
目前,希望表单有效,(a)如果输入了城市和州或(b)输入了邮政编码,任一组,并且表单有效。 如果两个组都为空,则表单无效。
这是基本的开始形式,目前 city 和 zip 都是可选的,试图结合这个正确的逻辑。
public editAddressForm: FormGroup;
this.editAddressForm = this.formBuilder.group({
'name': [null, [Validators.maxLength(100)]],
'phonenumber': [null, [Validators.maxLength(50)]],
'city': [null, [Validators.maxLength(32)]],
'state': [null,[Validators.maxLength(4)]],
'zipcode': [null, [Validators.maxLength(16)]]
})
尝试使用此资源,但它需要嵌套 city 和 zip,这不是必需的。
Angular, How to set validation for either of two field should validate in reaction form approach
【问题讨论】:
标签: angular typescript angular-reactive-forms angular8