]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Add match validator
authorRicardo Marques <rimarques@suse.com>
Mon, 18 Jun 2018 11:42:08 +0000 (12:42 +0100)
committerRicardo Marques <rimarques@suse.com>
Wed, 25 Jul 2018 15:16:50 +0000 (16:16 +0100)
Signed-off-by: Ricardo Marques <rimarques@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 fb682970c807095f39a9f84af8d7db58311902b5..3f111b555e7c41387ec88bd245fef81ebb0f8a3c 100644 (file)
@@ -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();
+    });
+  });
 });
index 875e18379e9456469b737aa4d9495528f49c25a1..37b933457ab15fd2010c5eb5cd4e484fd7242cd2 100644 (file)
@@ -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;
+    };
+  }
 }