I can't create dynamic FormGroup manually
See original GitHub issue🐞 bug report
Affected Package
The issue is caused by package @angular/....Is this a regression?
Yes, the previous version in which this bug was not present was: ....Description
this code not work fine at first time page load and fire exception
🔬 Minimal Reproduction
component.ts
this.contactForm = new FormGroup({
name: new FormControl(),
phones: new FormArray([new FormGroup({Phone: new FormControl('')})])
});
component.html
<div formArrayName="phones">
<div *ngFor="let item of phones.controls; let phoneIndex = index" [formGroupName]="phoneIndex">
<label>
Phone: <input formControlName="phone" />
</label>
<button type="button" (click)="deletePhone(phoneIndex)">Delete Phone</button>
</div>
<button type="button" (click)="addPhone()">Add Phone</button>
</div>
🔥 Exception or Error
Cannot find control with path: 'phones -> 0 -> phone'
🌍 Your Environment
Angular CLI: 8.3.23 Node: 12.14.1 OS: win32 x64
Anything else relevant? if I used formbuilder, it will work fine
this.contactForm = this.fb.group({
name: '',
address: '',
note: '',
phones: this.fb.array([this.fb.group({phone:''})])
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:16 (2 by maintainers)
Top Results From Across the Web
How can I generate FormControls dynamically inside a ...
To work with those "dias" array and include inputs to modify each value in case the user wants to, I've manually created a...
Read more >How to Build Dynamic Forms in Angular
First, declare a FormGroup field registerForm and create the method buildForm() to manually add every field in the formGroup. Copy. import { ...
Read more >Three Ways to Dynamically Alter your Form Validation in ...
The FormGroup class exposes an API that enables us to set validators dynamically. setValidators. We need to listen to optionB value changes and...
Read more >SelectControlValueAccessor - Angular
The ControlValueAccessor for writing select control values and listening to select control changes. The value accessor is used by the FormControlDirective , ...
Read more >Dynamically Add/Remove Validators in Angular Reactive Forms
For e.g. let's suppose you have a “County” field in your Reactive Form and you want to make it Required based on the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
See related bug report for Chrome 80 https://bugs.chromium.org/p/chromium/issues/detail?id=1049982 - _find method in forms.js uses Array.reduce
We totally have this same issue. We were alerted to it when testing in Chrome 80 Beta. The issue appears to be within the the _find function on the AbstractControl that is called withing the get(path: Array<string | number> | string) function. In that _find function it appears to break down in the .reduce.
Through some other testing I could prove that using this.form.get(‘xcontrol’) would return null, but if we called this.form.controls[‘xcontrol’] it is present.
Unfortunately from the html it is not possible to control how the directives get the controls. We were able to work around it creating an explicit formGroup instead of relying on formGroupName to create the formGroup. An example with your code would be:
Honestly, I feel like the issue is really a Chrome 80 issue, almost like a rendering problem. However, our app is too complicated to use to submit a bug to the chromium team, and I can’t reproduce it out of the context of our app. I am glad to see others are experiencing this also.