兩個欄位為二選一必填

想要請問一下
Credit Days和 Credit Amount
這兩個欄位可以擇一必填
2020-3-3 下午 10-22-12
以及Credit Amount這個欄位有填 Credit Currency就必填

請問可以用什麼方式做到呢?我是用REACTIVE FROM來做的

Reactive Form 可以動態的新增移除驗證規則

https://angular.io/api/forms/AbstractControl#setValidators

解法:
先訂閱Credit Amount的formcontrol
再用setValidators設定驗證,然後更新驗證updateValueAndValidity

TS檔:
在 ngOnInit() {}中

this.creditAmountFormGroup.controls.creditAmount.valueChanges.subscribe(res => {
  this.setRequireCreditAmount(res);
});

Function:

setRequireCreditAmount(res){
if (res) {
      this.setcreditCurrencyValidator();
    } else {
      this.clearcreditCurrencyValidator();    
    }
}


setcreditCurrencyValidator() {
this.formDataService.serviceFormGroup.controls.
      .get('creditCurrency')
      .setValidators([Validators.required]);
    this.formDataService.serviceFormGroup.controls.
      .get('creditCurrency')
      .updateValueAndValidity();
  }

clearcreditCurrencyValidator() {
this.formDataService.serviceFormGroup.controls.
      .get('creditCurrency')
      .clearValidators();
    this.formDataService.serviceFormGroup.controls.
      .get('creditCurrency')
      .updateValueAndValidity();
  }

如果有錯再請各位補充指教修正

這個問題我也想問 XD
等不到人回答

用 FormGroup 包起來,然後把驗證條件寫在 FormGroup 那一層