Skip to content

[Proposal] medatdata / extras property for AbstractControl #19686

Description

@mkurcius

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Currently there is no way to construct FormControl, FormGroup and FormArray and set metadata / extras info (ex. allowedValues etc.)

Expected behavior

Possibility to set metadata / extras info

What is the motivation / use case for changing the behavior?

Possibility to set metadata info for each specific control would be very helpful when writing forms. For example generating select options would looks like that:

<label for="control1">Control 1</label>
<select id="control1" [formControl]="control1">
  <option *ngFor="let option of control.metadata.allowedValues" [ngValue]="option">
    {{option.someProp}}
  </option>
</select>

<label for="control2">Control 2</label>
<select id="control2" [formControl]="control2">
  <option *ngFor="let option of control.metadata.allowedValues" [ngValue]="option">
    {{option.someProp}}
  </option>
</select>

Or if you have a form which is table but not all cells are controls, let say in each row you have cell with id (it is not a control just value for the user):

<table>
  <thead>
  <tr>
    <th>...</th>
    <th>...</th>
    <th>...</th>
  </tr>
  </thead>
  <tbody>
  <tr *ngFor="let control of controls"
      [formGroup]="control"
      [attr.id]="control.metadata.id">

    <td>{{control.metadata.id}}</td>
    <td><input formControlName="child1"></td>
    <td><input formControlName="child2"></td>
  </tr>
  </tbody>
</table>

And there may be many more usage examples

Proposal API

Setting this new property should be possible by using FormBuilder, FormControl, FormGroup and FormArray:

  • for FormBuilder#group pass as optional property in extra arg
  • for FormBuilder#array, FormGroup and FormArray pass as 4th optional arg
  • for FormBuilder#control and FormControl pass as optional property in formState arg

Example:

formBuilder.group({
  child1: {
    value: null, 
    metadata: { allowedValues: ['A', 'B', 'C'] }
  },
  child2: ''
  array: formBuilder.array([], null, null, {category: 'CUSTOMER_1'})
}, {
  metadata: { id: 1253 }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: formsfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationstate: Needs Design

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions