Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive - Angular reactive forms

I have the following template. I'm trying to get to grips with reactive forms but am having a problem.

<form [formGroup]="guestForm" novalidate>     <div class="panel-body">         <form>             <div class="col-md-12 col-sm-12">                 <div class="form-group col-md-3 col-sm-6">                     <label>First Name*  </label>                     <input formControlName="firstname" type="text" class="form-control input-sm">                 </div>             </div>         </form>     </div> </form> 

Then in my component I have:

@Component({     selector: 'guest-input',     templateUrl: './guest-input.component.html', }) export class GuestInputComponent implements OnInit {     @Input()     guest: Guest;      guestForm: FormGroup;          constructor(private _fb: FormBuilder) { }      ngOnInit() {         this.guestForm = this._fb.group({             firstname: ['test', [Validators.required, Validators.minLength(3)]]         });     } } 

This all looks fine to me but for some reason I am getting:

Error: Uncaught (in promise): 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).

I thought I had declared this in my <form>.

like image 804
Simon Avatar asked Apr 09 '17 11:04

Simon


People also ask

Can we use FormControlName without FormGroup?

Without a parent FormGroup, [formControl]="name" worked earlier because that directive can stand alone, that is, it works without being in a FormGroup. With a parent FormGroup, the name input needs the syntax formControlName=name in order to be associated with the correct FormControl in the class.

What is the purpose of the FormControlName directive?

FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name.

Which of the below option is an alternative to FormGroup to manage any number of unnamed controls?

FormArray is an alternative to FormGroup for managing any number of unnamed controls.

What directive do you use to configure an input in a reactive form?

Setup in reactive formslink The [formControl] directive links the explicitly created FormControl instance to a specific form element in the view, using an internal value accessor. The following component implements an input field for a single control, using reactive forms.


2 Answers

You have nested form tag inside form tag with FormGroup directive, remove it:

<form [formGroup]="guestForm" novalidate>     <div class="panel-body">         <form> -> Remove this             <div class="col-md-12 col-sm-12">                 <div class="form-group col-md-3 col-sm-6">                     <label>First Name*  </label>                     <input formControlName="firstname" type="text" class="form-control input-sm">                 </div>             </div>         </form> -> Remove this     </div> </form> 
like image 172
Stefan Svrkota Avatar answered Sep 20 '22 16:09

Stefan Svrkota


In modelDrivenForms, [formGroup]="guestForm" should be in the parent element

<div class="form-group col-md-3 col-sm-6" [formGroup]="guestForm">   <label>First Name*  </label>   <input formControlName="firstname" type="text" class="form-control input-sm"> </div> 
like image 43
Farzad.Kamali Avatar answered Sep 24 '22 16:09

Farzad.Kamali