From 645a61009b997347ae2e76326430ece728f39947 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stephan=20M=C3=BCller?= Date: Tue, 12 Jun 2018 12:16:50 +0200 Subject: [PATCH] mgr/dashboard: Add validateIf validator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Validate form control if condition is true with given validators Signed-off-by: Stephan Müller --- .../shared/validators/cd-validators.spec.ts | 42 +++++++++++++++++++ .../app/shared/validators/cd-validators.ts | 23 ++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.spec.ts index e2e1895f76109..fb682970c8070 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.spec.ts @@ -126,4 +126,46 @@ describe('CdValidators', () => { expect(y.valid).toBeTruthy(); }); }); + + describe('validate if condition', () => { + let form: FormGroup; + + beforeEach(() => { + form = new FormGroup({ + x: new FormControl(3), + y: new FormControl(5) + }); + CdValidators.validateIf(form.get('x'), () => ((form && form.get('y').value) || 0) > 10, [ + CdValidators.custom('min', (x) => x < 7), + CdValidators.custom('max', (x) => x > 12) + ]); + }); + + it('should test min error', () => { + const x = form.get('x'); + const y = form.get('y'); + expect(x.valid).toBeTruthy(); + y.setValue(11); + x.updateValueAndValidity(); + expect(x.hasError('min')).toBeTruthy(); + }); + + it('should test max error', () => { + const x = form.get('x'); + const y = form.get('y'); + expect(x.valid).toBeTruthy(); + y.setValue(11); + x.setValue(13); + expect(x.hasError('max')).toBeTruthy(); + }); + + it('should test valid number with validation', () => { + const x = form.get('x'); + const y = form.get('y'); + expect(x.valid).toBeTruthy(); + y.setValue(11); + x.setValue(12); + expect(x.valid).toBeTruthy(); + }); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.ts index b8fc514757df3..875e18379e945 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/validators/cd-validators.ts @@ -73,4 +73,27 @@ export class CdValidators { return null; }; } + + /** + * Validate form control if condition is true with validators. + * + * @param {AbstractControl} formControl + * @param {Function} condition + * @param {ValidatorFn[]} validators + */ + static validateIf( + formControl: AbstractControl, + condition: Function, + validators: ValidatorFn[] + ) { + formControl.setValidators((control: AbstractControl): { + [key: string]: any; + } => { + const value = condition.call(this); + if (value) { + return Validators.compose(validators)(control); + } + return null; + }); + } } -- 2.39.5