Created
May 18, 2020 06:09
-
-
Save xiongemi/97edfd730976e0a9742ef19e46e178a9 to your computer and use it in GitHub Desktop.
dynamic validations - manually clear validators and set validators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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