From: Ricardo Marques Date: Mon, 18 Jun 2018 11:42:08 +0000 (+0100) Subject: mgr/dashboard: Add match validator X-Git-Tag: v14.0.1~771^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=fa3b70c06b5e2f604ecb45a0ace83e9894bbff46;p=ceph.git mgr/dashboard: Add match validator Signed-off-by: Ricardo Marques --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.spec.ts index fb682970c807..3f111b555e7c 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.spec.ts @@ -168,4 +168,35 @@ describe('CdValidators', () => { 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(); + }); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.ts index 875e18379e94..37b933457ab1 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.ts @@ -96,4 +96,20 @@ export class CdValidators { 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; + }; + } }