Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 710 Bytes

conditional-validators.md

File metadata and controls

32 lines (26 loc) · 710 Bytes

Conditional Validators

<input
  type="checkbox"
  formControlName="isOtherCurrency" 
  #otherCurrency
  (change)="otherCurrencyCheckChanged(otherCurrency.checked)"
/> Other currency than USD

<span>Currency:</span>
<input
  type="text"
  formControlName="currency"
/>
public otherCurrencyCheckChanged(isChecked: boolean): void {
  const currencyControl = this.form.controls.currency;

  if (isChecked) {
    currencyControl.setValidators([Validators.required]);
  } else {
    currencyControl.clearValidators();
  }

  currencyControl.updateValueAndValidity();
}

If we only pass 1 validator, it does not have to be given in an array: currencyControl.setValidators(Validators.required).