Skip to content

Instantly share code, notes, and snippets.

@xiongemi
Created May 18, 2020 06:09
Show Gist options
  • Save xiongemi/97edfd730976e0a9742ef19e46e178a9 to your computer and use it in GitHub Desktop.
Save xiongemi/97edfd730976e0a9742ef19e46e178a9 to your computer and use it in GitHub Desktop.
dynamic validations - manually clear validators and set validators
this.subscription.add(
this.deliveryPageForm.get('isShippingSame').valueChanges.subscribe((isShippingSame: boolean) => {
if (isShippingSame) {
this.deliveryPageForm.get('shippingAddress').reset();
this.deliveryPageForm.get('shippingAddress').clearValidators();
} else {
this.deliveryPageForm.get('shippingAddress').setValidators([Validators.required]);
}
})
);
this.subscription.add(
this.deliveryPageForm.get('createAccount').valueChanges.subscribe((createAccount: boolean) => {
if (createAccount) {
this.deliveryPageForm.get(['account', 'password']).setValidators([Validators.required]);
this.deliveryPageForm.get(['account', 'confirmPassword']).setValidators([Validators.required]);
this.deliveryPageForm.get(['account', 'password']).reset();
this.deliveryPageForm.get(['account', 'confirmPassword']).reset();
} else {
this.deliveryPageForm.get(['account', 'password']).clearValidators();
this.deliveryPageForm.get(['account', 'confirmPassword']).clearValidators();
}
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment