# All files / src/app/shared/forms cd-form-group.ts

100% Statements 24/24
88.89% Branches 8/9
100% Functions 10/10
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    7698x

    98x

    718x

    718x

    98x
    48030x

    4x

    48026x

    98x
    51790x

    37446x

    3760x

    33686x

    34223x

    98x
    6710x

    98x
    487x

    98x
    10998x
    10998x

    98x
     import {
      AbstractControl,
      AbstractControlOptions,
      AsyncValidatorFn,
      FormGroup,
      NgForm,
      ValidatorFn
    } from '@angular/forms';

    /**
     * CdFormGroup extends FormGroup with a few new methods that will help form development.
     */
    export class CdFormGroup extends FormGroup {
      constructor(
        public controls: { [key: string]: AbstractControl },
        validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null,
        asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null
      ) {
        super(controls, validatorOrOpts, asyncValidator);
      }

      /**
       * Get a control out of any control even if its nested in other CdFormGroups or a FormGroup
       */
      get(controlName: string): AbstractControl {
        const control = this._get(controlName);
        if (!control) {
          throw new Error(`Control '${controlName}' could not be found!`);
        }
        return control;
      }

      _get(controlName): AbstractControl {
        return (
          super.get(controlName) ||
          Object.values(this.controls)
            .filter((c) => c.get)
            .map((form) => {
              if (form instanceof CdFormGroup) {
                return form._get(controlName);
              }
              return form.get(controlName);
            })
            .find((c) => Boolean(c))
        );
      }

      /**
       * Get the value of a control
       */
      getValue(controlName: string): any {
        return this.get(controlName).value;
      }

      /**
       * Sets a control without triggering a value changes event
       *
       * Very useful if a function is called through a value changes event but the value
       * should be changed within the call.
       */
      silentSet(controlName: string, value: any) {
        this.get(controlName).setValue(value, { emitEvent: false });
      }

      /**
       * Indicates errors of the control in templates
       */
      showError(controlName: string, form: NgForm, errorName?: string): boolean {
        const control = this.get(controlName);
        return (
          (form.submitted || control.dirty) &&
          (errorName ? control.hasError(errorName) : control.invalid)
        );
      }
    }