Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular FormBuilder multiple FormGroups

Tags:

I have a form with 2 FromGroups (User & Address)

I get the following error:

 core.es5.js:1084 ERROR Error: Uncaught (in promise): Error: Cannot find control with name: 'street' 

when I use this class

export class FormBuilderComp {         addUserFrom: FormGroup;         constructor( @Inject(FormBuilder) fb: FormBuilder) {             this.addUserFrom = fb.group({                 userGroup: fb.group({                     name: ['', Validators.required],                     email: ['', Validators.required],                     phone: ['', Validators.required]                 }),                 addressGroup: fb.group({                     street: ['', Validators.required],                     suite: ['', Validators.required],                     city: ['', Validators.required],                     zipCode: ['', Validators.required]                 })             });         }     } 

...But if I take out one of the nested FormGroups as in

  export class FormBuilderComp {         addUserFrom: FormGroup;         constructor( @Inject(FormBuilder) fb: FormBuilder) {             this.addUserFrom = fb.group({                 userGroup: fb.group({                     name: ['', Validators.required],                     email: ['', Validators.required],                     phone: ['', Validators.required]                 }),                 street: ['', Validators.required],                 suite: ['', Validators.required],                 city: ['', Validators.required],                 zipCode: ['', Validators.required]              });         }     } 

The error disappears.

Is there some rule about not having more than one nested FromGroup??

Here's the html in case it's relevant

 <form [formGroup]="addUserFrom">         <fieldset formGroupName="userGroup">                 <legend>User</legend>             <div class="form-group">                 <label for="name">Name</label>                 <input                      type="text"                      class="form-control"                      id="name"                      formControlName="name">             </div>             <div class="form-group">                 <label for="email">Email</label>                 <input                      type="text"                      class="form-control"                      id="email"                      formControlName="email">             </div>             <div class="form-group">                 <label for="phone">Phone</label>                 <input                      type="text"                      class="form-control"                      id="phone"                      formControlName="phone">             </div>         </fieldset>         <fieldset fromGroupName="addressGroup">             <legend>Address</legend>         <div class="form-group">                 <label for="street">Street</label>                 <input                      type="text"                      class="form-control"                      id="street"                      formControlName="street">             </div>             <div class="form-group">                 <label for="suite">Suite</label>                 <input                      type="text"                      class="form-control"                      id="suite"                      formControlName="suite">             </div>             <div class="form-group">                 <label for="city">City</label>                 <input                      type="text"                      class="form-control"                      id="city"                      formControlName="city">             </div>             <div class="form-group">                 <label for="zipCode">Zip Code</label>                 <input                      type="text"                      class="form-control"                      id="zipCode"                      formControlName="zipCode">             </div>         </fieldset>          <button>Submit</button>     </form> 
like image 970
ShanieMoonlight Avatar asked Apr 19 '17 20:04

ShanieMoonlight


People also ask

What is difference between FormBuilder and FormControl?

The official docs describe it as: Creating form control instances manually can become repetitive when dealing with multiple forms. The FormBuilder service provides convenient methods for generating controls. So basically saying that FormBuilder is a service that is trying to help us reduce boiler-plate code.

What is the use of FormBuilder in Angular?

The FormBuilder provides syntactic sugar that shortens creating instances of a FormControl , FormGroup , or FormArray . It reduces the amount of boilerplate needed to build complex forms.

What is the difference between FormControl and formControlName?

[formControl] assigns a reference to the FormControl instance you created to the FormControlDirective . formControlName assigns a string for the forms module to look up the control by name.


1 Answers

There is typo in your html, change fromGroupName to formGroupName.

like image 137
ulubeyn Avatar answered Sep 20 '22 17:09

ulubeyn