expect(x.valid).toBeTruthy();
});
});
+
+ describe('match', () => {
+ let form: FormGroup;
+
+ beforeEach(() => {
+ form = new FormGroup({
+ x: new FormControl(),
+ y: new FormControl()
+ });
+ });
+
+ it('should error when values are different', () => {
+ const x = form.get('x');
+ const y = form.get('y');
+ x.setValue('aaa');
+ y.setValue('aab');
+ CdValidators.match('x', 'y')(form);
+ expect(x.hasError('match')).toBeFalsy();
+ expect(y.hasError('match')).toBeTruthy();
+ });
+
+ it('should not error when values are equal', () => {
+ const x = form.get('x');
+ const y = form.get('y');
+ x.setValue('aaa');
+ y.setValue('aaa');
+ CdValidators.match('x', 'y')(form);
+ expect(x.hasError('match')).toBeFalsy();
+ expect(y.hasError('match')).toBeFalsy();
+ });
+ });
});
return null;
});
}
+
+ /**
+ * Validate if control1 and control2 have the same value.
+ * Error will be added to control2.
+ *
+ * @param {string} control1
+ * @param {string} control2
+ */
+ static match(control1: string, control2: string): ValidatorFn {
+ return (control: AbstractControl): { [key: string]: any } => {
+ if (control.get(control1).value !== control.get(control2).value) {
+ control.get(control2).setErrors({ ['match']: true });
+ }
+ return null;
+ };
+ }
}