Dynamic forms to auto create reactive forms based on a schema #43219
Labels
area: forms
canonical
This issue represents a canonical design issue in Angular.
feature: under consideration
Feature request for which voting has completed and the request is now under consideration
feature
Issue that requests a new feature
forms: dynamic
P4
A relatively minor issue that is not relevant to core functions
Milestone
Which @angular/* package(s) are relevant/releated to the feature request?
forms
Description
Problem
Currently the only way to add additional controls is by using methods such as the
addControl
onFormGroup
orinsert
onFormArray
. However this approach introduces a significant boilerplate for apps since they must entirely manage their own dynamic forms. These are reactive form objects that perform dynamic CRUD operations on their child controls. There's an abundance of issues in this repo that are rooted in lack of a managed solution for dynamic form, that is, a form that can auto-manage its children.FormBuilder
is a fantastic tool, but it lack automated CRUD operations for controls.Proposed solution
To address this we can extend the
FormBuilder
and add:AbstractControl
, reason is discussed further belowauto
onFormBuilder
which will take a schema and returns a dynamic form builder. This schema will mainly represent the structure of auto createdAbstractControl
including all the additional parameters required to initialize anAbstractControl
.So why not just write a standard recursive method called
auto
onFormBuilder
?This is actually a problem for a few reasons. Firstly there's the case of immediate subscriptions:
This can break if the
auto
method hasn't created the control yet. So instead dynamic form builder (which is returned by theauto
) will output the proper error since it'll wrapvalueChange
with a_isCreated
check.Another issue is the data size. Passing an astronomical array data to create a dynamic
FormArray
can slow down the page which will only result in more issues to pop up in this repo. Instead we add a constraint to the dynamic form to restrict the amount of nodes that can go in theAbstractControl
tree and output a few suggestions to the developer such as to introduce pagination on their data array.What about the performance?
As mentioned above we'll add constrains to the
auto
for these common cases, but the recursion itself is anO(n)
wheren
is the total number of nested keys in any given data object that's passed in.Alternatives considered
I'm open to alternatives, but I can't think of any
The text was updated successfully, but these errors were encountered: