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

Getting Error: Cannot find control '0' when implementing FormArray as shown in Google doc guide #9251

Closed
1 task done
qdouble opened this issue Jun 16, 2016 · 8 comments
Closed
1 task done
Assignees
Labels
area: forms feature Issue that requests a new feature
Milestone

Comments

@qdouble
Copy link

qdouble commented Jun 16, 2016

  • I'm submitting a ...
  • bug report

Current behavior
When implementing FormArray as shown in guide: https://docs.google.com/document/u/1/d/1RIezQqE4aEhBRmArIAS1mRIZtWFf6JxN_7B4meyWK0Y/pub

I get an error >browser_adapter.js:77 Error: Cannot find control '0'

You can see the issue here: https://plnkr.co/edit/ir19tuktTqcfGeG8pu2G?p=preview

  • Angular version: 2.0.0-rc.2 ... edited
  • Browser: [all]
  • Language: [TypeScript]
@ericmartinezr
Copy link
Contributor

ericmartinezr commented Jun 16, 2016

@qdouble you have to match the formControlName in the FormGroup

<div *ngFor="let city of cityArray.controls; let i=index" >
    <input [formControlName]="i">
</div>

form = new FormGroup({
     cities: this.cityArray,
     0 : new Control(),
     1 : new Control(),
     etc...
   });

Since you're using the index as the name of the control, the controls key should be numbers.

I would say it's more something missing in the doc than a bug, although I may be wrong.

cc @kara

@qdouble
Copy link
Author

qdouble commented Jun 16, 2016

@ericmartinezr that doesn't appear to work either. I see they've labeled it, so I suppose it is indeed a bug.

@kara
Copy link
Contributor

kara commented Jun 16, 2016

The planned updates to forms are still very much in progress, so a few things in the proposal doc aren't implemented yet and are slated for the next release (namely, formArrayName and form level updateValue). I've updated the original proposal doc to clarify these are still to come.

I'll keep this issue open so it's easier to track its completion. Sorry for the confusion!

@kara kara closed this as completed in c03e1f2 Jun 25, 2016
@kara kara added feature Issue that requests a new feature and removed action: review labels Jun 28, 2016
@tom10271
Copy link

Is it possible to make FormArray fallback to its controls[0] for controls[1] when controls[1] is missing?

I am building a Angular 2 Form generator. It generates proper FormGroup, FormArray, FormControl but I am stucked as when generating form according to object schema I don't know how many objects contained inside FormArray.

Generated Form equivalent:

this.form = new FormGroup({
    myArr: new FormArray([
        new FormControl('')
    ])
})

When I set value -> this.form.setValue({ myArray: ['val1', 'val2'] }), it returns error: main.js:80313 EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot find form control at index 1

I understand the reason why, but it can be fixed if Angular 2 Form can fall back to myArr.controls[0] for all sibilings Form.

Thanks

@ZokiPokiDev
Copy link

Maybe this will help you as an idea

const controlName = new FormControl(null);
    const controlEmail = new FormControl(null, [
      Validators.pattern(EMAIL_REGEX)
    ]);
    const controlPhone = new FormControl(null);
    const group = new FormGroup({
      'name': controlName,
      'email': controlEmail,
      'phone': controlPhone 
    });
    (<FormArray>this.editEvent.get('contact')).push(group);

@Swapnil25594
Copy link

Swapnil25594 commented Oct 27, 2018

I was ecountering same issue. This way it helped me.

this.invoiceForm = this._fb.group({
      patterns: this._fb.array([new FormControl('')]),
    });
<form [formGroup]="invoiceForm">
          <div formArrayName="patterns"> 
            <div *ngFor="let pattern of invoiceForm.controls.patterns.controls ; let i = index">
              <br>
              <h6>Pattern {{i+1}}</h6>
              <button type="submit" class='"btn btn-warning pull-right" (click)="deletePatterns(i)"> Remove</button>
              <div>
                <label>Value</label>
                <input formControlName="{{i}}" placeholder="Text" class="form-control">
              </div>
            </div>
          </div>
 </form> 

@upardhi
Copy link

upardhi commented Jun 7, 2019

Hi Guys,

I am also facing the same issue. When I click on add hotspot it is throwing an error "No value accessor for 'hotspots -> 0'"

Please help me

http://plnkr.co/edit/p2NCCb?p=preview

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: forms feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

8 participants