3 AbstractControlOptions,
8 } from '@angular/forms';
11 * CdFormGroup extends FormGroup with a few new methods that will help form development.
13 export class CdFormGroup extends FormGroup {
15 public controls: { [key: string]: AbstractControl },
16 validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null,
17 asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null
19 super(controls, validatorOrOpts, asyncValidator);
23 * Get a control out of any control even if its nested in other CdFormGroups or a FormGroup
25 get(controlName: string): AbstractControl {
26 const control = this._get(controlName);
28 throw new Error(`Control '${controlName}' could not be found!`);
33 _get(controlName): AbstractControl {
35 super.get(controlName) ||
36 Object.values(this.controls)
39 if (form instanceof CdFormGroup) {
40 return form._get(controlName);
42 return form.get(controlName);
44 .find((c) => Boolean(c))
49 * Get the value of a control
51 getValue(controlName: string): any {
52 return this.get(controlName).value;
56 * Sets a control without triggering a value changes event
58 * Very useful if a function is called through a value changes event but the value
59 * should be changed within the call.
61 silentSet(controlName: string, value: any) {
62 this.get(controlName).setValue(value, { emitEvent: false });
66 * Indicates errors of the control in templates
68 showError(controlName: string, form: NgForm, errorName?: string): boolean {
69 const control = this.get(controlName);
71 (form.submitted || control.dirty) &&
72 (errorName ? control.hasError(errorName) : control.invalid)