Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize form group in Angular 4?

I have the following ngOnInit method:

ngOnInit() {
    this.countries = this.sharedService.getCountries();
    this.shopService.getCurrentShopFromCache().then(shop => {
        this.shop = shop;
        this.myFormGroup = this.fb.group({
            name: [this.shop.name[this.shop.defaultLanguage.code], [Validators.required]],
            address: [this.shop.address.address],
            city: [this.shop.address.city],
            state: [this.shop.address.state],
            country: [this.shop.address.country, [Validators.required]],
            phone: [this.shop.phone],
            email: [this.shop.email],
            website: [this.shop.social.website],
            twitter: [this.shop.social.twitter],
            facebook: [this.shop.social.facebook],
            instagram: [this.shop.social.instagram],
            foursquare: [this.shop.social.foursquare]
        });
    }
    );
}

I'm getting

formGroup expects a FormGroup instance. Please pass one in.

Where am I wrong?

UPDATE:

 <form *ngIf="shop" class="m-form m-form--fit m-form--label-align-right" [formGroup]="myFormGroup" novalidate>
                       ... 
like image 392
Burak Avatar asked Oct 16 '17 14:10

Burak


People also ask

What is the use of form group in angular?

FormGroup is one of the three fundamental building blocks used to define forms in Angular, along with FormControl and FormArray. When instantiating a FormGroup, pass in a collection of child controls as the first argument. The key for each child registers the name for the control.

How to enable validation in formgroup in angular?

We can access its value using get () method. To validate a form control in FormGroup, angular provides Validators class. We can use it to enable validation in form control as given below. To check validity of form control, we can write a method for a form control as given below. Now in HTML template we can access validity state as userName.invalid.

What is formcontrol in angular?

One of the three essential building blocks in Angular forms — along with FormGroup and FormArray — FormControl extends the AbstractControl class, which enables it to access the value, validation status, user interactions, and events. FormGroup is used with FormControl to track the value and validate the state of form control.

How do I set all values in an angular form?

In Angular, you can set values to individual form groups or set all FormGroup values at once. You do not need to supply all values here; fields whose values were not set will be unaffected. To set all FormGroup values simultaneously, use setValue: What is FormBuilder in Angular?


1 Answers

You have to instantiate formgroup immediately on component creation, i.e. in the constructor, otherwise angular just cannot find what to bind template properties to.

UPDATE

Rephrasing: you have to instantiate form group before template gets rendered by angular. It's stricter than angular 1.x and throws an error if it cannot evaluate expression in template binding at the time of html form rendering.

Since you're using *ngIf="shop" in the template I'd say it means that shop turns not null before then() gets executed - maybe initially, maybe by some other function - it's not in the code you provided, so I can't point it out.

What you're trying to do is to initialize form with some data fetched by some service. That's totally fine - but it's still no reason to postpone creation of controls. Just create them in the constructor and set values later in the ngOnInit using FormGroup's setValue(), patchValue() or reset() - depending on what exactly you need. Below is just the idea, you'll need to adjust it to your form structure.

app.component.ts

import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

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

    formGroup: FormGroup;

    constructor(fb: FormBuilder) {
        this.formGroup = fb.group({
            title: fb.control('initial value', Validators.required)
        });
    }

    ngOnInit(): void {
        this.formGroup.reset({title: 'new value'});
    }

}

app.component.html

<form [formGroup]="formGroup">
    <input type="text" formControlName="title">
</form>
like image 144
Alexander Leonov Avatar answered Sep 28 '22 04:09

Alexander Leonov