formControlName must be used with a parent formGroup directive
Asked Answered
M

4

7

While creating model driven form, i am getting an error: Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class).

Please tell me whats going wrong in this code.

app.component.html

<div class="col-md-6">
  <form  (ngSubmit)="saveSession(newSessionForm.value)" autocomplete="off">
    <div class="form-group">
      <label for="sessionName">Session Name:</label>
      <input formControlName="name" id="sessionName" type="text" class="form-control" placeholder="session name..." />
    </div>
    <div class="form-group">
      <label for="abstract">Abstract:</label>
      <textarea formControlName="abstract" id="abstract" rows=3 class="form-control" placeholder="abstract..."></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
  </form>
</div>

app.component.ts

export class CreateSession {

    newSessionForm:FormGroup;
    abstract : FormControl;
    name : FormControl;

    ngOInInit(){
        this.name = new FormControl('', Validators.required)
        this.abstract = new FormControl('', Validators.required)

        this.newSessionForm = new FormGroup({
            name:this.name,
            abstract: this.abstract
        })         
    }


    saveSession(formValues){
        console.log(formValues);
    }
Muskrat answered 22/8, 2017 at 15:57 Comment(1)
It's solved here! #43306475Thessalonian
F
14

Angular is waiting for FormGroupDirective on any of parent elements. So:

<form [formGroup]="newSessionForm" ...
  <input formControlName="name"
  ...
  <input formControlName="abstract"

If you want to use FormControl without formGroup you can use FormControlDirective instead:

<input [formControl]="name"
...
<input [formControl]="abstract"
Fuliginous answered 22/8, 2017 at 16:0 Comment(1)
How to access this value in typescript ?Salicylate
G
4

If your FormGroup is in a parent component, and Form control in a child component, you have to declare viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }] in the child component like this:

@Component({
  selector: 'child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css'],
  viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }]
})

export class ChildComponent implements OnInit {
...
Georgy answered 16/11, 2020 at 8:38 Comment(0)
S
1

Can u try this way

<div class="col-md-12" formArrayName="educations">
</div>

ts file code

educations: this.formBuilder.array([]),

Or

<form [formGroup]="manageOrderForm">

</form>

ts file code

import { Router } from '@angular/router';

@Component({
  selector: 'app-order',
  templateUrl: './order.component.html',
  styleUrls: ['./order.component.css']
})
export class OrderComponent implements OnInit {

  manageOrderForm: any;
}
Selfdiscipline answered 12/5, 2021 at 5:48 Comment(0)
F
0

First you have to specify the formGroup , which one is going to drive the form controls binded with the inputs.

<form [formGroup]="newSessionForm" (ngSubmit)="saveSession(newSessionForm.value)" autocomplete="off">
<div class="form-group">
  <label for="sessionName">Session Name:</label>
  <input formControlName="name" id="sessionName" type="text" class="form-control" placeholder="session name..." />
</div>
<div class="form-group">
  <label for="abstract">Abstract:</label>
  <textarea formControlName="abstract" id="abstract" rows=3 class="form-control" placeholder="abstract..."></textarea>
</div>
<button type="submit" class="btn btn-primary">Save</button>

Frisby answered 22/8, 2017 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.