import { FormGroup } from '@angular/forms'; export const confirmValidator = (formName: string) => (group: FormGroup): { [key: string]: { formName: string } } | null => { const { _parent: parent } = group as any; if (parent) { if (parent.controls[formName].value === group.value) { return null; } checkChangeFormName(parent, formName, group); } return { confirm: { formName } }; }; const checkChangeFormName = (parent, formName, group) => { parent.controls[formName].valueChanges.subscribe((value) => { if (value !== group.value) { group.setErrors({ confirm: { formName } }); } else if (group.errors) { delete group.errors.confirm; if (!Object.keys(group.errors).length) { group.setErrors(null); } } }); };