expect(validatorFn(form.controls['y'])).toEqual({ required: true });
});
});
+
+ describe('custom validation', () => {
+ let form: FormGroup;
+
+ beforeEach(() => {
+ form = new FormGroup({
+ x: new FormControl(3, CdValidators.custom('odd', (x) => x % 2 === 1)),
+ y: new FormControl(
+ 5,
+ CdValidators.custom('not-dividable-by-x', (y) => {
+ const x = (form && form.get('x').value) || 1;
+ return y % x !== 0;
+ })
+ )
+ });
+ });
+
+ it('should test error and valid condition for odd x', () => {
+ const x = form.get('x');
+ x.updateValueAndValidity();
+ expect(x.hasError('odd')).toBeTruthy();
+ x.setValue(4);
+ expect(x.valid).toBeTruthy();
+ });
+
+ it('should test error and valid condition for y if its dividable by x', () => {
+ const y = form.get('y');
+ y.updateValueAndValidity();
+ expect(y.hasError('not-dividable-by-x')).toBeTruthy();
+ y.setValue(6);
+ y.updateValueAndValidity();
+ expect(y.valid).toBeTruthy();
+ });
+ });
});
return success ? { required: true } : null;
};
}
+
+ /**
+ * Custom validation by passing a name for the error and a function as error condition.
+ *
+ * @param {string} error
+ * @param {Function} condition - a truthy return value will trigger the error
+ * @returns {ValidatorFn}
+ */
+ static custom(error: string, condition: Function): ValidatorFn {
+ return (control: AbstractControl): { [key: string]: any } => {
+ const value = condition.call(this, control.value);
+ if (value) {
+ return { [error]: value };
+ }
+ return null;
+ };
+ }
}