Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add meta data for formControl?

I have array of forms FormGroup;

let forms = [this.form1, this.form2];

Each form has several controls: FormControl.

How to add additional data to FormControl for example type of element (input, textarea, select)?

For example I have form:

export class ProfileEditorComponent {
  profileForm = this.fb.group({
    firstName: [''],
    lastName: [''],
    address: this.fb.group({
      street: [''],
      city: [''],
      state: [''],
      zip: ['']
    }),
  });

How can I know that under zip field should be input or select?

How to iterate profileForm in template with nested form?


1 Answers

Answered before the edit of the question

UPDATE

The following is called module augmentation as described here or in the official documentation

This allows for augmenting (e.g. adding methods pr properties) classes from third party modules

ORIGINAL

We had the requirement to have a warning validators which is not provided by angular. What we did was the following in our typings.d.ts file which has to reside in our applications root directory.

import { AbstractControl } from '@angular/forms';

declare module '@angular/forms' {

  export interface AbstractControl {
    warnings: ValidationErrors | null;
  }
}

AbstractControl.prototype.warnings = ''; //add a default value instead of ''

ValidationErrors is a custom class of ours.

You could do something like

declare module '@angular/forms' {

  export interface AbstractControl {
    elementType: string; // input, textarea, select
  }
}

With that you all your AbstractControls in your application would have the property elementType available.

You could also use FormControl instead of AbstractControl

like image 127
Arikael Avatar answered Nov 02 '25 17:11

Arikael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!