Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] medatdata / extras property for AbstractControl #19686

Open
mkurcius opened this issue Oct 12, 2017 · 8 comments
Open

[Proposal] medatdata / extras property for AbstractControl #19686

mkurcius opened this issue Oct 12, 2017 · 8 comments
Labels
area: forms feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature state: Needs Design
Milestone

Comments

@mkurcius
Copy link

mkurcius commented Oct 12, 2017

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 }
})
@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@mkurcius
Copy link
Author

Is there any news on this? :)

@kobvel
Copy link

kobvel commented Jul 5, 2019

Any updates here?

@jorroll
Copy link
Contributor

jorroll commented Oct 15, 2019

For those interested, I created a proposal to update ReactiveFormsModule to address a few outstanding issues, including this one (#31963). The proposal is a breaking update (proposed as ReactiveFormsModule2), which is definitely not necessary to address this issue specifically--but I figured folks interested in this issue, and improving the ReactiveFormsModule in general, might have useful feedback.

In addition to adding a data property to AbstractControl (for storing arbitrary data), it also updates the AbstractControl API so all changes to the abstract control are emitted from a changes property (allowing you to subscribe to updates for any abstract control property). It also allows adding arbitrary metadata to any state change emitted from the changes property. This might be useful if you wanted to have special handling of value changes which were triggered by a specific service (as opposed to user input). You can learn more in #31963.

@kdryzek
Copy link

kdryzek commented Nov 12, 2019

It may be achieved by simple extending of FormControl, then each of custom inputs may have its own custom model extending DefaultFormControlModel. However native support for such feature would be highly appreciated.

export interface DefaultFormControlModel<T> {
    value?: T;
    required?: boolean;
    label?: string;
    disabled?: boolean;
    readonly?: boolean;
    asyncValidators?: Validator[];
    validators?: Validator[];
}

export abstract class CustomFormControl<T extends DefaultFormControlModel<T['value']>> extends FormControl {
    public model: T = { } as any;

    protected constructor(defaultFormControlModel: T) {
        super({
                value: defaultFormControlModel.value,
                disabled: !!defaultFormControlModel.disabled,
            },
            getValidators(defaultFormControlModel.validators),
            getAsyncValidators(defaultFormControlModel.asyncValidators),
        );
        Object.assign(this.model, defaultFormControlModel);
    }

    public setModelProperty(name: string, value: any) {
        this.model[name] = value;
    }
}

@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jun 4, 2021
@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 4, 2021

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 24, 2021

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

@angular-robot angular-robot bot added the feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors label Jun 24, 2021
@AndrewKushnir AndrewKushnir added feature: under consideration Feature request for which voting has completed and the request is now under consideration and removed freq1: low feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase labels Jun 25, 2021
@tahir-jamil
Copy link

Mata data is a best features when you are creating dynamic reactive forms also where form group is also dynamically created by form controls such as i need to send
productID, attributeId, attributeValue
hear is this example i have to use attribute Value and in from control but as we don't have any mata data option so when submitting the form i don't know what the current AttributeValue is related to What productId or what attribute Id,
We only have the list of Attributes and if the BindValue is id instead of object then we only know the list of ids of attributes which is useless for me unless i assign related id to related productId and AttributeId

Mata data can fix this problem very easily while creating Form Control i can set all mata data related to that object with attributeValue and at the end i have my array of object already set

Please reconsider this feature and add mata data support
Votes are always insufficient because this feature is required we very less frequency but its power frequency is much much higher .

@SoaringColinLaws
Copy link

I just ran into a use case in which I need meta data on AbstractControl, all for the same reasons as other users here on this thread. Additional information for the control would allow us to dynamically create forms, without the need to manually create templates every time we need a form. In the meantime, we'll have to find a way to extend or modify Angular's AbstractControl type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: forms feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature state: Needs Design
Projects
None yet
Development

No branches or pull requests

10 participants