Skip to content

Reactive Forms get list of controls in array #65062

@fabioemoutinho

Description

@fabioemoutinho

Which @angular/* package(s) are relevant/related to the feature request?

@angular/forms, signal forms

Description

Similar to https://angular.dev/api/forms/FormArray#get, a getAll method or some other name would be useful to return a list of controls that match a given path.

A common use case would be, for example, to get specific controls of form groups in a form array using a path.
example:

form = this.fb.array([
    this.fb.group({
        prop1: this.fb.control(...);
        prop2: this.fb.control(...);
    }), // imagine there are multiple controls with this structure
]);

then, to get all prop1 within the array, you could:

const controls: FormControl<Type>[] = this.form.getAll('*.prop1');

and it is especially useful when creating validators that accept paths and should be recalculated when any of the inputs in an array are changed. For example, a sum of values should be less than 100, I want the validator to be triggered when any control in the form array changes, it's a validator that is applicable to multiple controls, thus it should be part of the array validators.

I also hope a path selector like this (with paths like arrayControl.*.controlName, as in take all controlName controls within arrayControl) would be available in new signal forms, with the added performance benefit that only matched controls (and not all array controls) will trigger the validators to re-run.

Proposed solution

I volunteer myself to make an attempt at implementing it for Reactive forms, similar to .get in case this is approved. Thanks!

Alternatives considered

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions