Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mat-form-field must contain a MatFormFieldControl and already imported

I get the error ERROR mat-form-field must contain a MatFormFieldControl which should mean that I need to import MatFormFieldModule in my nearest parent module. But I already did, so I don't understand what the problem is...

template

<form [formGroup]="form" (ngSubmit)="handleSubmit()">
    <mat-form-field>
        <input matInput placeholder="Name" formControlName="name" />
    </mat-form-field>

    <mat-form-field>
        <textarea
            matInput
            placeholder="Active Description"
            formControlName="activeDescription"
        ></textarea>
    </mat-form-field>

    <mat-form-field>
        <textarea
            matInput
            placeholder="Inactive Description"
            formControlName="inactiveDescription"
        ></textarea>
    </mat-form-field>

    <mat-form-field>
        <mat-checkbox formControlName="active">Active</mat-checkbox>
    </mat-form-field>
</form>

module

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule, MatCheckboxModule, MatFormFieldModule, MatInputModule, MatSelectModule } from '@angular/material';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { PaymentConfigSelectorComponent } from './payment-config-selector/payment-config-selector.component';
import { PaymentMethodComponent } from './payment-method/payment-method.component';
import { PaymentMethodsComponent } from './payment-methods/payment-methods.component';
import { PaymentRoutingModule } from './payment-routing.module';
import { PaymentEffects } from './payment.effects';
import { paymentReducer } from './payment.reducer';
import { PaymentSmartComponent } from './smart/payment-smart.component';

@NgModule({
  declarations: [PaymentSmartComponent, PaymentMethodsComponent, PaymentConfigSelectorComponent, PaymentMethodComponent ],
  imports: [
    CommonModule,
    PaymentRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    EffectsModule.forFeature([PaymentEffects]),
    StoreModule.forFeature(
      'payment',
      paymentReducer,
    ),
    MatFormFieldModule, MatInputModule, MatSelectModule, MatCheckboxModule, MatButtonModule,
  ],
  providers: [
  ],
})
export class PaymentModule { }
like image 635
olefrank Avatar asked Jan 31 '19 12:01

olefrank


2 Answers

Actually the error doesn't mean a module import problem, but that you're using the mat-form-field without a valid control.

The problem is here :

<mat-form-field>
    <mat-checkbox formControlName="active">Active</mat-checkbox>
</mat-form-field>

MatChebox isn't intended to be contained in a mat-form-field but yeah its name is not very clear ... Keep just in mind that mat-form-field is the component that handles underline + hint + floating label + errors, etc ...

The list of supported child component is : input / textarea / select / chip-list (see https://material.angular.io/components/form-field/overview)

like image 190
Jscti Avatar answered Oct 06 '22 00:10

Jscti


I fixed the issue by adding a hidden input field.

<mat-form-field>
 <mat-label>My Label</mat-label>
 <input matInput [hidden]="true" [(ngModel)]="myModel" name="myModel">
 <mat-checkbox class="ml-3" [(ngModel)]="myModel" name="myModel"></mat-checkbox>
</mat-form-field>
like image 31
Sergej Morgulis Avatar answered Oct 06 '22 01:10

Sergej Morgulis