components icon indicating copy to clipboard operation
components copied to clipboard

Error: mat-form-field must contain a MatFormFieldControl.

Open icvanee opened this issue 9 years ago • 39 comments

Bug, feature request, or proposal:

When doing a karma/jasmine test I get an error on the tag. This in combination with a

<mat-form-field> <mat-select name="country" placeholder="Country" [(ngModel)]="address.countryName" class="input-full-width"> <mat-option *ngFor="let country of countries | orderBy: 'name'" [value]="country.name">{{ country.name }}</mat-option> </mat-select> </mat-form-field>

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Using angular 4.4.5 material 2.0.0-beta.12

Is there anything else we should know?

When running the project it's working okay, but just the test gives an error and fails.

icvanee avatar Oct 19 '17 07:10 icvanee

I have the same issue... Angular 4.4.6, material 2.0.0-beta.12

tomchovanec avatar Oct 19 '17 12:10 tomchovanec

You have to add to app.module.ts:

import {MatInputModule} from '@angular/material';
@NgModule({
  imports: [
  ...
    MatInputModule
  ...
]


tomchovanec avatar Oct 19 '17 12:10 tomchovanec

As said, in the normal situation it work. I have added de MatInputModule in the imports section. And when testing in the .spec.ts file I have imported the MatInputModule also. Even in the TestBed.configureTestingModule imports section is is added too.

icvanee avatar Oct 19 '17 13:10 icvanee

You have to add matInput to input

<div class="fields-container">
      <mat-form-field>
        <input matInput placeholder="Input">
      </mat-form-field>

    </div>

Abdullah-FAl avatar Oct 21 '17 15:10 Abdullah-FAl

I know a matInput must be inside a mat-form-field. But the issue is that I don't want an input field, but a select.

icvanee avatar Oct 23 '17 07:10 icvanee

@icvanee to be sure, have you imported MatSelectModule into your testing module? And you don't have an ngIf on your mat-select, right?

willshowell avatar Oct 23 '17 12:10 willshowell

shame that was it. I really really thought I had added that. Imissed it when I was migrating also with the removal of MatModule. Thx for noticing!

icvanee avatar Oct 23 '17 12:10 icvanee

I have hadded MatSelectModule and I still get the error.

This is my component:

<mat-form-field>
  <mat-select [placeholder]="receivedLabel" [(ngModel)]="value" (blur)="onBlur()" class="select-country-main">
    <ng-template ngFor let-country [ngForOf]="countries" [ngForTrackBy]="trackByCountryId">
      <mat-option *ngIf="!isExcluded(country)" [value]="country">
        <country-flag [country]="country"></country-flag>
      </mat-option>
    </ng-template>
  </mat-select>
</mat-form-field>

jagomf avatar Nov 20 '17 19:11 jagomf

I had an issue similar to yours @jagomf For me it was corrected when I modified the ngIf condition of my mat-select, I had value !== null in the ngIf, when I changed it to just value this fixed it. I would suggest setting an excluded property on your country objects and checking this in the ngIf, rather than calling the isExcluded method on your ngIf. You might also notice performance improvements if you do this, as referencing a method call in your html template causes it to be called on every change detection round.

mattmcardle avatar Dec 24 '17 00:12 mattmcardle

seems. like. the. error. message. could. be. improved.

ORESoftware avatar Feb 13 '18 20:02 ORESoftware

i also have this same error and trying to fix but can't. Any help please.

<mat-form-field [floatLabel]="options.value.floatLabel"> Tenant Name

  <mat-form-field 
      [hideRequiredMarker]="options.value.hideRequired"
      [floatLabel]="options.value.floatLabel">
    <mat-select required>
      <mat-option mdInput *ngFor="let room of roomType" value="option">{{room.name}}</mat-option>
    </mat-select>
    <mat-placeholder>-Select Room Type-</mat-placeholder>
  </mat-form-field>

but it keeps giving

FusePricingStyle2Component.html:17 ERROR Error: mat-form-field must contain a MatFormFieldControl. at getMatFormFieldMissingControlError (form-field.es5.js:111) at MatFormField._validateControlChild (form-field.es5.js:637) at MatFormField.ngAfterContentInit (form-field.es5.js:392) at callProviderLifecycles (core.js:12699) at callElementProvidersLifecycles (core.js:12673) at callLifecycleHooksChildrenFirst (core.js:12656) at checkAndUpdateView (core.js:13806) at callViewAction (core.js:14153) at execComponentViewsAction (core.js:14085) at checkAndUpdateView (core.js:13808)

SekoViper avatar Mar 21 '18 09:03 SekoViper

@SekoViper That's because you're using mdInput instead of matInput. Replace it.

EdricChan03 avatar Apr 04 '18 12:04 EdricChan03

Thanks very much. I have been able to fix that. My next problem is to implement timer in a quiz app am building. How do I go about it? Thanks.

On Wed, 4 Apr 2018, 12:03 p.m. Edric Chan, [email protected] wrote:

@SekoViper https://github.com/SekoViper That's because you're using mdInput instead of matInput. Replace it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/material2/issues/7898#issuecomment-378576235, or mute the thread https://github.com/notifications/unsubscribe-auth/AgRdXDTvjL86uR9nZr7aWXd1IcBZjU7xks5tlLargaJpZM4P-1f7 .

SekoViper avatar Apr 05 '18 07:04 SekoViper

i also have this same error and trying to fix but can't. Any help please.


    <div class="row litre">
      <div class="col-md-6">
    <form class="litre">
        <mat-form-field class="example-full-width">
            <label *ngIf="!editMode">{{litre}}</label>
            <input matInput placeholder="Input" *ngIf="editMode" [(ngModel)]="litre"  [ngModelOptions]="{standalone: true}">
        </mat-form-field>
  </form>
  </div>

screenshot 39

sriharsha6 avatar Apr 11 '18 10:04 sriharsha6

Try this way:

    <mat-form-field class="example-full-width">
        <input matInput placeholder="Input" [disabled]="!editMode" [(ngModel)]="litre"  [ngModelOptions]="{standalone: true}">
    </mat-form-field>

mucbits avatar Apr 11 '18 11:04 mucbits

Thanks very much,

I want edit mode_edit save check_circle in a single button

screenshot 40


    <div class="row litre">
      <div class="col-md-6">
    <form class="litre">
        <mat-form-field class="example-full-width">
             <input matInput placeholder="Input" [disabled]="!editMode" [(ngModel)]="litre"  [ngModelOptions]="{standalone: true}">
</mat-form-field>
        </mat-form-field>
  </form>
  </div>

sriharsha6 avatar Apr 11 '18 13:04 sriharsha6

Be sure that you don't have any *ngIf on your input / select

pierreclr avatar Apr 16 '18 08:04 pierreclr

can u send that code by using *ngIf on your input / select

sriharsha6 avatar Apr 18 '18 09:04 sriharsha6

Im receiving same issue numerous times within my custom mattable

ERROR Error: mat-form-field must contain a MatFormFieldControl.
    at getMatFormFieldMissingControlError (form-field.es5.js:111)
    at MatFormField._validateControlChild (form-field.es5.js:637)
    at MatFormField.ngAfterContentChecked (form-field.es5.js:427)
    at callProviderLifecycles (core.js:12702)
    at callElementProvidersLifecycles (core.js:12673)
    at callLifecycleHooksChildrenFirst (core.js:12656)
    at checkAndUpdateView (core.js:13806)
    at callViewAction (core.js:14153)
    at execComponentViewsAction (core.js:14085)
    at checkAndUpdateView (core.js:13808)

MEANSTACKDIGITAL avatar Apr 23 '18 07:04 MEANSTACKDIGITAL

mat-form-field must contain a MatFormFieldControl this is missing? in app.module.ts MatInputModule

Ukmandal avatar Apr 24 '18 09:04 Ukmandal