Last active
May 19, 2020 06:42
-
-
Save xiongemi/5c3eaabd8c83001e836ca8dc930adc94 to your computer and use it in GitHub Desktop.
An angular form group validator for dynamic validation
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
import { FormGroup } from '@angular/forms'; | |
import { DeliveryPageFormValue } from '../models/delivery-page-form-value.interface'; | |
export function deliveryPageFromValidator(deliveryPageFrom: FormGroup): { [key: string]: any } | null { | |
const formValue: DeliveryPageFormValue = deliveryPageFrom.value; | |
return deliveryPageFromValueValidator(formValue); | |
} | |
export function deliveryPageFromValueValidator(deliveryPageFromValue: DeliveryPageFormValue): { [key: string]: any } | null { | |
const errors = {}; | |
if (!deliveryPageFromValue.isShippingSame && !deliveryPageFromValue.shippingAddress) { | |
errors['shippingAddress'] = true; | |
} | |
if (deliveryPageFromValue.createAccount) { | |
if (!deliveryPageFromValue.account.password) { | |
errors['password'] = true; | |
} | |
if (!deliveryPageFromValue.account.confirmPassword) { | |
errors['confirmPassword'] = true; | |
} else if (deliveryPageFromValue.account.password !== deliveryPageFromValue.account.confirmPassword) { | |
errors['passwordMatch'] = true; | |
} | |
} | |
return Object.keys(errors).length ? errors : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment