Skip to content

Instantly share code, notes, and snippets.

@xiongemi
Last active May 19, 2020 06:42
Show Gist options
  • Save xiongemi/5c3eaabd8c83001e836ca8dc930adc94 to your computer and use it in GitHub Desktop.
Save xiongemi/5c3eaabd8c83001e836ca8dc930adc94 to your computer and use it in GitHub Desktop.
An angular form group validator for dynamic validation
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