]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Enhance CdValidators.validateIf method
authorTiago Melo <tmelo@suse.com>
Tue, 15 Jan 2019 15:49:14 +0000 (15:49 +0000)
committerTiago Melo <tmelo@suse.com>
Tue, 5 Feb 2019 12:02:33 +0000 (12:02 +0000)
Signed-off-by: Tiago Melo <tmelo@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-validators.ts

index fdc5428921d2b35570be16d481ec34384b5e1329..e2fe10e236050698de303052f76074ca71789abe 100644 (file)
@@ -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', () => {
index 620c4b65eb620d8d16e2b96f7ef1e87c4ec27aee..d1cc8ed6280b828bd557db40452fdd6c3829e8be 100644 (file)
@@ -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 });
+      });
+    });
   }
 
   /**