From: Tiago Melo Date: Tue, 15 Jan 2019 15:49:14 +0000 (+0000) Subject: mgr/dashboard: Enhance CdValidators.validateIf method X-Git-Tag: v14.1.0~208^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=4598bfb34b4476b3f0a235baa1945ce154c68cf9;p=ceph.git mgr/dashboard: Enhance CdValidators.validateIf method Signed-off-by: Tiago Melo --- 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 fdc5428921d..e2fe10e2360 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 @@ -1,5 +1,5 @@ import { fakeAsync, tick } from '@angular/core/testing'; -import { FormControl } from '@angular/forms'; +import { FormControl, Validators } from '@angular/forms'; import { of as observableOf } from 'rxjs'; @@ -375,6 +375,34 @@ describe('CdValidators', () => { formHelper.setValue('x', 12); formHelper.expectValid('x'); }); + + it('should validate automatically if dependency controls are defined', () => { + CdValidators.validateIf( + form.get('x'), + () => ((form && form.getValue('y')) || 0) > 10, + [Validators.min(7), Validators.max(12)], + undefined, + [form.get('y')] + ); + + formHelper.expectValid('x'); + formHelper.setValue('y', 13); + formHelper.expectError('x', 'min'); + }); + + it('should always validate the permanentValidators', () => { + CdValidators.validateIf( + form.get('x'), + () => ((form && form.getValue('y')) || 0) > 10, + [Validators.min(7), Validators.max(12)], + [Validators.required], + [form.get('y')] + ); + + formHelper.expectValid('x'); + formHelper.setValue('x', ''); + formHelper.expectError('x', 'required'); + }); }); describe('match', () => { 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 620c4b65eb6..d1cc8ed6280 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 @@ -138,9 +138,21 @@ export class CdValidators { * * @param {AbstractControl} formControl * @param {Function} condition - * @param {ValidatorFn[]} validators + * @param {ValidatorFn[]} conditionalValidators List of validators that should only be tested + * when the condition is met + * @param {ValidatorFn[]} permanentValidators List of validators that should always be tested + * @param {AbstractControl[]} watchControls List of controls that the condition depend on. + * Every time one of this controls value is updated, the validation will be triggered */ - static validateIf(formControl: AbstractControl, condition: Function, validators: ValidatorFn[]) { + static validateIf( + formControl: AbstractControl, + condition: Function, + conditionalValidators: ValidatorFn[], + permanentValidators: ValidatorFn[] = [], + watchControls: AbstractControl[] = [] + ) { + conditionalValidators = conditionalValidators.concat(permanentValidators); + formControl.setValidators( ( control: AbstractControl @@ -149,11 +161,20 @@ export class CdValidators { } => { const value = condition.call(this); if (value) { - return Validators.compose(validators)(control); + return Validators.compose(conditionalValidators)(control); + } + if (permanentValidators.length > 0) { + return Validators.compose(permanentValidators)(control); } return null; } ); + + watchControls.forEach((control: AbstractControl) => { + control.valueChanges.subscribe(() => { + formControl.updateValueAndValidity({ emitEvent: false }); + }); + }); } /**