Unfortunately I can’t just comment on some already good answers as I don’t have the SO points yet, however, there are 3 key modules that you’ll need to make sure you are importing into your component’s parent module, aside from what you have to import into your component directly. I wanted to briefly share them and highlight what they do.
- MatInputModule
- MatFormFieldModule
- ReactiveFormsModule
The first two are for Angular Material. A lot of people new to Angular Material will instinctively come across one of these and not realize that building a form requires both.
So what’s the difference between them?
MatFormFieldModule encompasses all the different types of form fields that Angular Material has available. This is more of a high level module for form fields in general, whereas the MatInputModule is specifically for ‘input’ field types, as opposed to select boxes, radio buttons, etc.
The third item on the above list is Angular’s Reactive Forms Module. The Reactive Forms Module is responsible for a ton of under-the-hood Angular love. If you are going to work with Angular, I highly recommend that you spend some time reading Angular Docs. They have all of your answers. Building applications rarely DOESN’T involve forms, and more often than not, your application will involve reactive forms. For that reason, please take the time to read these two pages.
Angular Docs: Reactive Forms
Angular Docs: Reactive Forms Module
The first doc ‘Reactive Forms’ will be your most powerful weapon as you get started, and the second doc will be your most power weapon as you get on to more advanced applications of Angular Reactive Forms.
Remember, these need to be imported directly into your component’s module, not the component you are using them in. If I remember correctly, in Angular 2, we imported the entire set of Angular Material modules in our main app module, and then imported what we needed in each of our components directly. The current method is much more efficient IMO because we are guaranteed to import the entire set of Angular Material modules if we only use a few of them.
I hope this provides a bit more insight into building forms with Angular Material.
If you are using Angular material modules you might have encountered mat-form-field must contain a MatFormFieldControl
error while using MatFormFieldModule
.
This error occurs when you have missed to add a form field control to your form field.
Table of Contents
what are form field controls?
The native elements like <input>
or <textarea>
, which are placed inside mat-form-field
acts as form field controls.
If your mat-form-field
contains an <input>
or <textarea>
element, make sure you’ve added the matInput
directive to it and have imported MatInputModule
other wise you will get mat-form-field must contain a MatFormFieldControl
error in your application.
And other components that can act as a form field control includes <mat-select>
, <mat-chip-list>
and any custom form field controls you’ve created.
Fixing mat-form-field must contain a MatFormFieldControl
error.
To fix the error, add MatInputModule
and MatFormFieldModule
inside a necessary module or app.module.ts
.
Or as explained in Angular material tutorial add all material modules in a common module and add it to app.module.ts
file so that we can use them across the application.
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from "@angular/material/form-field";
@NgModule({
imports: [
MatFormFieldModule,
MatInputModule
]
})
export class AppModule { }
And next make sure you have added matInput
directive to mat-form-field
control.
<mat-form-field appearance="fill">
<mat-label>Input</mat-label>
<input placeholder="Name">
</mat-form-field>
The above code throws error because we have not added matInput
directive.
<mat-form-field appearance="fill">
<mat-label>Input</mat-label>
<input matInput placeholder="Name">
</mat-form-field>
And matInput
is case-sensitive.
So if you have a spelling mistake in matInput
or capital case(MatInput
) or small case letters(matinput
) in matInput
, still you will encounter the error.
<mat-form-field appearance="fill">
<mat-label>Input</mat-label>
<input matinput placeholder="Name">
</mat-form-field>
mat-form-field
control input should not contain *ngIf
If you are using *ngIf
on mat-form-field control i.e., <input>
tag, you will get mat-form-field must contain a MatFormFieldControl
error.
The below code will not work.
<mat-form-field>
<mat-label>Input</mat-label>
<input matInput *ngIf="condition"/>
</mat-form-field>
Instead of that you should use *ngIf
on mat-form-field
element.
<mat-form-field *ngIf="condition">
<mat-label>Input</mat-label>
<input matInput />
</mat-form-field>
Summary
To fix mat-form-field must contain a MatFormFieldControl
error follow the below steps
- Import
MatInputModule
andMatFormFieldModule
- Add
matInput
directive to themat-form-field
control. i.e.,input
ortextarea
- Check
matInput
spelling. - Don’t use
*ngIf
onmat-form-field
control instead use it onmat-form-field
element
If you are using Angular material in your project and if you have any forms in the application then you must be using Angular Material Form Field Component <mat-form-field>
from MatFormFieldModule
And of the common error that we usually get is while working with Angular Material Form Field is: mat-form-field must contain a MatFormFieldControl.
In this article we will see how to fix “mat-form-field must contain a MatFormFieldControl” error.
What is form field and form field controls?
<mat-form-field>
is a component used to wrap several Angular Material components and apply common Text field styles such as the underline, floating label, and hint messages.
The elements like <input>
or <textarea>
, which are placed inside mat-form-field
acts as form field controls.
This error usually occurs in 4 scenarios when we missed to import module for specific component.
Problem 1: MatInputModule or MatFormFieldModule is not imported in AppModule
import MatInputModule
and MatFormFieldModule
inside module i.e. app.module.ts
so that we can use all the component from that module across the application.
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from "@angular/material/form-field";
//other imports
@NgModule({
imports: [
MatFormFieldModule,
MatInputModule
...
...
]
})
export class AppModule { }
Problem 2: matInput – spellings mistake / forget to add
Make sure that you have added matInput
and it is case-sensitive so check if there’s any spelling mistake or not.
Won’t work
<mat-form-field>
<input type="text" placeholder="Input">
</mat-form-field>
Will Work
<mat-form-field>
<input matinput="" type="text" placeholder="Input">
</mat-form-field>
Problem 3: mat-form-field
control input should not contain *ngIf
Make sure that you don’t have any condition on input
tag that is going to be false in any case because mat-form-field
looks for matInput
inside it. Instead put *ngIf
on mat-form-field
tag.
Won’t work
<mat-form-field>
<input matinput="" *ngif="condition">
</mat-form-field>
Will Work
<mat-form-field>
<input matinput="">
</mat-form-field>
Problem 4: Still compiler giving ERROR
If angular compiler is still giving an error after applying solution of all of the above given problems then you must try with restarting the app using the following command.
ng serve
Conclusion
To fix mat-form-field must contain a MatFormFieldControl
error check if you are missing any of the below steps.
- Import
MatInputModule
andMatFormFieldModule
- Add
matInput
directive to themat-form-field
control. i.e.,input
ortextarea
- Check
matInput
spelling or forgot to add. - Make sure you don’t use
*ngIf
onmat-form-field
control instead use it onmat-form-field
element
Also Read:
- JavaScriptArray Methods
- JavaScript Console Methods
- Angular Material Grid Layout Example
- Angular Material Card Example
- Angular Material List Example
- Tailwind CSS Setup
mat-form-field must contain a MatFormFieldControl
Questions : mat-form-field must contain a MatFormFieldControl
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00
777
We are trying to build our own questions uvdos angular-material form-field-Components at our Company. We are questions uvdos angular-material trying to wrap material design’s Components questions uvdos angular-material like this:
field:
<mat-form-field>
<ng-content></ng-content>
<mat-hint align="start"><strong>{{hint}}</strong> </mat-hint>
<mat-hint align="end">{{message.value.length}} / 256</mat-hint>
<mat-error>This field is required</mat-error>
</mat-form-field>
textbox:
<field hint="hint">
<input matInput
[placeholder]="placeholder"
[value]="value"
(change)="onChange($event)"
(keydown)="onKeydown($event)"
(keyup)="onKeyup($event)"
(keypress)="onKeypress($event)">
</field>
Usage:
<textbox value="test" hint="my hint"></textbox>
This results in approximately this:
<textbox placeholder="Personnummer/samordningsnummer" value="" ng-reflect-placeholder="Personnummer/samordningsnummer">
<field>
<mat-form-field class="mat-input-container mat-form-field>
<div class="mat-input-wrapper mat-form-field-wrapper">
<div class="mat-input-flex mat-form-field-flex">
<div class="mat-input-infix mat-form-field-infix">
<input _ngcontent-c4="" class="mat-input-element mat-form-field-autofill-control" matinput="" ng-reflect-placeholder="Personnummer/samordningsnummer" ng-reflect-value="" id="mat-input-2" placeholder="Personnummer/samordningsnummer" aria-invalid="false">
<span class="mat-input-placeholder-wrapper mat-form-field-placeholder-wrapper"></span>
</div>
</div>
<div class="mat-input-underline mat-form-field-underline">
<span class="mat-input-ripple mat-form-field-ripple"></span>
</div>
<div class="mat-input-subscript-wrapper mat-form-field-subscript-wrapper"></div>
</div>
</mat-form-field>
</field>
</textbox>
But I’m getting «mat-form-field must contain questions uvdos angular-material a MatFormFieldControl» in the console. I questions uvdos angular-material guess this has to do with mat-form-field not questions uvdos angular-material directly containing a matInput-field. But it questions uvdos angular-material is containing it, it’s just withing the questions uvdos angular-material ng-content projection.
Here is a questions uvdos angular-material blitz:
https://stackblitz.com/edit/angular-xpvwzf
Total Answers 30
28
Answers 1 : of mat-form-field must contain a MatFormFieldControl
I had this issue. I imported questions uvdos angular-material MatFormFieldModule at my main module, questions uvdos angular-material but forgot to add MatInputModule to the questions uvdos angular-material imports array, like so:
import { MatFormFieldModule, MatInputModule } from '@angular/material';
@NgModule({
imports: [
MatFormFieldModule,
MatInputModule
]
})
export class AppModule { }
More info here.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
mRahman
2
Answers 2 : of mat-form-field must contain a MatFormFieldControl
Problem 1: MatInputModule
Not imported
import MatInputModule and questions uvdos angular-material MatFormFieldModule inside module i.e. questions uvdos angular-material app.module.ts
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from "@angular/material/form-field";
@NgModule({
imports: [
MatFormFieldModule,
MatInputModule
]
})
export class AppModule { }
Problem 2: matInput — spellings mistake / forget to add
Be sure to add matInput and it is questions uvdos angular-material case-sensitive.
<mat-form-field>
<input matInput type="text" />
</mat-form-field>
Problem 3: Invalid *ngIf placement
Check that you don’t have any condition questions uvdos angular-material on input tag that is going to be false questions uvdos angular-material in any case because mat-form-field looks questions uvdos angular-material for matInput inside it. Instead put questions uvdos angular-material *ngIf on mat-form-field tag.
<mat-form-field>
<input matInput *ngIf="someCondition"> // don't add condition here, else add in mat-form-field tag
</mat-form-field>
credit to @William Herrmann for pointing questions uvdos angular-material out this problem#3
Problem 4: Still compiler giving ERROR
if angular compiler still giving error questions uvdos angular-material after fixing above given problems then questions uvdos angular-material you must try with restarting the app.
ng serve
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
karim
2
Answers 3 : of mat-form-field must contain a MatFormFieldControl
Import MatInputModule in your module.ts questions uvdos angular-material file and it will solve the problem.
import { MatInputModule } from '@angular/material/input';
The statement after it is the old questions uvdos angular-material answer.
Unfortunately content projection into questions uvdos angular-material mat-form-field is not supported questions uvdos angular-material yet.
Please track the following github questions uvdos angular-material issue to get the latest news about it.
By now the only solution for you is questions uvdos angular-material either place your content directly into questions uvdos angular-material mat-form-field component or implement a questions uvdos angular-material MatFormFieldControl class thus creating questions uvdos angular-material a custom form field component.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
3
Answers 4 : of mat-form-field must contain a MatFormFieldControl
This can also happen if you have a questions uvdos angular-material proper input within a mat-form-field, questions uvdos angular-material but it has a ngIf on it. E.g.:
<mat-form-field>
<mat-chip-list *ngIf="!_dataLoading">
<!-- other content here -->
</mat-chip-list>
</mat-form-field>
In my case, mat-chip-list is supposed to questions uvdos angular-material «appear» only after its data is loaded. questions uvdos angular-material However, the validation is performed and questions uvdos angular-material mat-form-field complains with
mat-form-field must contain a questions uvdos angular-material MatFormFieldControl
To fix it, the control must be there, so questions uvdos angular-material I have used [hidden]:
<mat-form-field>
<mat-chip-list [hidden]="_dataLoading">
<!-- other content here -->
</mat-chip-list>
</mat-form-field>
An alternative solution is proposed by questions uvdos angular-material Mosta: move *ngIf for mat-form-field:
<mat-form-field *ngIf="!_dataLoading">
<mat-chip-list >
<!-- other content here -->
</mat-chip-list>
</mat-form-field>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
3
Answers 5 : of mat-form-field must contain a MatFormFieldControl
Quoting from the official documentation questions uvdos angular-material here:
Error: mat-form-field must contain a questions uvdos angular-material MatFormFieldControl
This error occurs when you have not questions uvdos angular-material added a form field control to your form questions uvdos angular-material field. If your form field contains a questions uvdos angular-material native <input> or <textarea> questions uvdos angular-material element, make sure you’ve added the questions uvdos angular-material matInput directive to it and have questions uvdos angular-material imported MatInputModule. Other questions uvdos angular-material components that can act as a form field questions uvdos angular-material control include <mat-select>, questions uvdos angular-material <mat-chip-list>, and any custom questions uvdos angular-material form field controls you’ve created.
Learn more about creating a «custom form questions uvdos angular-material field control» here
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
yousuf
2
Answers 6 : of mat-form-field must contain a MatFormFieldControl
If anyone got stuck with this error questions uvdos angular-material after attempting to nest a questions uvdos angular-material <mat-checkbox>, be of good cheer! questions uvdos angular-material It doesn’t work inside a questions uvdos angular-material <mat-form-field> tag.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
karim
1
Answers 7 : of mat-form-field must contain a MatFormFieldControl
import {MatInputModule} from '@angular/material/input';
@NgModule({
imports: [
MatInputModule
],
exports: [
MatInputModule
]
})
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
yousuf
2
Answers 8 : of mat-form-field must contain a MatFormFieldControl
I had this issue too, and I have questions uvdos angular-material <mat-select> element in my questions uvdos angular-material template, when I import the questions uvdos angular-material MatSelectModule into Module, it works, questions uvdos angular-material it doesn’t make sense, but still I hope questions uvdos angular-material it can help you.
import { MatSelectModule } from '@angular/material/select';
imports: [
BrowserModule,
BrowserAnimationsModule,
MatSidenavModule,
MatButtonModule,
MatIconModule,
MatToolbarModule,
MatFormFieldModule,
MatProgressSpinnerModule,
MatInputModule,
MatCardModule,
MatSelectModule
],
<div class="example-container">
<mat-form-field>
<input matInput placeholder="Input">
</mat-form-field>
<mat-form-field>
<textarea matInput placeholder="Textarea"></textarea>
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Select">
<mat-option value="option">Option</mat-option>
</mat-select>
</mat-form-field>
</div>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
3
Answers 9 : of mat-form-field must contain a MatFormFieldControl
if anyone is trying to nest a questions uvdos angular-material <mat-radio-group> inside a questions uvdos angular-material <mat-form-field>
like below, you questions uvdos angular-material will get this error
<mat-form-field>
<!-- <mat-label>Image Position</mat-label> -->
<mat-radio-group aria-label="Image Position" [(ngModel)]="section.field_1">
<mat-radio-button value="left">Left</mat-radio-button>
<mat-radio-button value="right">Right</mat-radio-button>
</mat-radio-group>
</mat-form-field>
remove the parent questions uvdos angular-material <mat-form-field> tags
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
4
Answers 10 : of mat-form-field must contain a MatFormFieldControl
I’m not sure if it could be this simple questions uvdos angular-material but I had the same issue, changing questions uvdos angular-material «mat-input» to «matInput» in the input questions uvdos angular-material field resolved the problem. In your case questions uvdos angular-material I see «matinput» and it’s causing my app questions uvdos angular-material to throw the same error.
<input _ngcontent-c4="" class="mat-input-element mat-form-field-autofill-control" matinput="" ng-reflect-placeholder="Personnummer/samordningsnummer" ng-reflect-value="" id="mat-input-2" placeholder="Personnummer/samordningsnummer" aria-invalid="false">
«matinput»
«matInput»
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
5
Answers 11 : of mat-form-field must contain a MatFormFieldControl
If all of the main code questions uvdos angular-material structure/configuration answers above questions uvdos angular-material don’t solve your issue, here’s one more questions uvdos angular-material thing to check: make sure that you questions uvdos angular-material haven’t in some way invalidated your questions uvdos angular-material <input> tag.
For instance, I received the same questions uvdos angular-material mat-form-field must contain a questions uvdos angular-material MatFormFieldControl error message after questions uvdos angular-material accidentally putting a required questions uvdos angular-material attribute after a self-closing slash /, questions uvdos angular-material which effectively invalidated my questions uvdos angular-material <input/>. In other words, I did questions uvdos angular-material this (see the end of the input tag questions uvdos angular-material attributes):
wrong:
<input matInput type="text" name="linkUrl" [formControl]="listingForm.controls['linkUrl']" /required>
right:
<input matInput type="text" name="linkUrl" [formControl]="listingForm.controls['linkUrl']" required/>
If you make that mistake or something questions uvdos angular-material else to invalidate your <input>, questions uvdos angular-material then you could get the mat-form-field questions uvdos angular-material must contain a MatFormFieldControl questions uvdos angular-material error. Anyway, this is just one more questions uvdos angular-material thing to look for as you’re debugging.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
2
Answers 12 : of mat-form-field must contain a MatFormFieldControl
Angular 9+ …Material 9+
I have noticed 3 mistakes can can give questions uvdos angular-material rise to the same error:
- Ensure you have imported MatFormFieldModule and MatInputModule in the app.module or the module.ts where your components are being imported(in case of nested modules)
- When you wrap material buttons or checkbox in mat-form-field. See the list of material components that can be wrapped with mat-form-field mat-form-field
- When you fail to include matInput in your tag. i.e <input matInput type=»number» step=»0.01″ formControlName=»price» />
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
karim
5
Answers 13 : of mat-form-field must contain a MatFormFieldControl
I had accidentally removed the matInput questions uvdos angular-material directive from the input field which questions uvdos angular-material caused the same error.
eg.
<mat-form-field>
<input [readOnly]="readOnly" name="amount" formControlName="amount" placeholder="Amount">
</mat-form-field>
fixed code
<mat-form-field>
<input matInput [readOnly]="readOnly" name="amount" formControlName="amount" placeholder="Amount">
</mat-form-field>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
3
Answers 14 : of mat-form-field must contain a MatFormFieldControl
Unfortunately I can’t just comment on questions uvdos angular-material some already good answers as I don’t questions uvdos angular-material have the SO points yet, however, there questions uvdos angular-material are 3 key modules that you’ll need to questions uvdos angular-material make sure you are importing into your questions uvdos angular-material component’s parent module, aside from questions uvdos angular-material what you have to import into your questions uvdos angular-material component directly. I wanted to briefly questions uvdos angular-material share them and highlight what they do.
- MatInputModule
- MatFormFieldModule
- ReactiveFormsModule
The first two are for Angular Material. questions uvdos angular-material A lot of people new to Angular Material questions uvdos angular-material will instinctively come across one of questions uvdos angular-material these and not realize that building a questions uvdos angular-material form requires both.
So what’s the difference between them?
MatFormFieldModule encompasses all the questions uvdos angular-material different types of form fields that questions uvdos angular-material Angular Material has available. This is questions uvdos angular-material more of a high level module for form questions uvdos angular-material fields in general, whereas the questions uvdos angular-material MatInputModule is specifically for questions uvdos angular-material ‘input’ field types, as opposed to questions uvdos angular-material select boxes, radio buttons, etc.
The third item on the above list is questions uvdos angular-material Angular’s Reactive Forms Module. The questions uvdos angular-material Reactive Forms Module is responsible for questions uvdos angular-material a ton of under-the-hood Angular love. questions uvdos angular-material If you are going to work with Angular, I questions uvdos angular-material highly recommend that you spend some questions uvdos angular-material time reading Angular Docs. They have questions uvdos angular-material all of your answers. Building questions uvdos angular-material applications rarely DOESN’T involve questions uvdos angular-material forms, and more often than not, your questions uvdos angular-material application will involve reactive forms. questions uvdos angular-material For that reason, please take the time questions uvdos angular-material to read these two pages.
Angular Docs: Reactive Forms
Angular Docs: Reactive Forms Module
The first doc ‘Reactive Forms’ will be questions uvdos angular-material your most powerful weapon as you get questions uvdos angular-material started, and the second doc will be your questions uvdos angular-material most power weapon as you get on to more questions uvdos angular-material advanced applications of Angular questions uvdos angular-material Reactive Forms.
Remember, these need to be imported questions uvdos angular-material directly into your component’s module, questions uvdos angular-material not the component you are using them in. questions uvdos angular-material If I remember correctly, in Angular 2, questions uvdos angular-material we imported the entire set of Angular questions uvdos angular-material Material modules in our main app module, questions uvdos angular-material and then imported what we needed in each questions uvdos angular-material of our components directly. The current questions uvdos angular-material method is much more efficient IMO questions uvdos angular-material because we are guaranteed to import the questions uvdos angular-material entire set of Angular Material modules questions uvdos angular-material if we only use a few of them.
I hope this provides a bit more insight questions uvdos angular-material into building forms with Angular questions uvdos angular-material Material.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
1
Answers 15 : of mat-form-field must contain a MatFormFieldControl
The same error can occur if you have a questions uvdos angular-material mat slide within a mat from field as the questions uvdos angular-material only element
in my case I had
<mat-form-field>
<mat-slide-toggle [(ngModel)]="myvar">
Some Text
</mat-slide-toggle>
</mat-form-field>
This might happen if you had several questions uvdos angular-material attributes within the questions uvdos angular-material <mat-form-field>
Simple solution questions uvdos angular-material was to move the slide toggle to the root
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
2
Answers 16 : of mat-form-field must contain a MatFormFieldControl
You have missed matInput directive in questions uvdos angular-material your input tag.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
yousuf
5
Answers 17 : of mat-form-field must contain a MatFormFieldControl
New updated MatInput Module import is:
import {MatInputModule} from '@angular/material/input';
As per Angular Materials API
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
5
Answers 18 : of mat-form-field must contain a MatFormFieldControl
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
don’t forget to add at first of questions uvdos angular-material <tabe></table>
<mat-form-field>
<mat-label>Filter</mat-label>
<input matInput (keyup)="applyFilter($event)" placeholder="Ex. Helium" #input>
</mat-form-field>
don’t forget to add at last of questions uvdos angular-material <tabe></table>
<tr class="mat-row" *matNoDataRow>
<td class="mat-cell" colspan="4">
No data matching the filter "{{input.value}}"
</td>
</tr>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
4
Answers 19 : of mat-form-field must contain a MatFormFieldControl
I had the problem occuring in one of my questions uvdos angular-material Karma tests
As most answers already pointed at, questions uvdos angular-material missing modules can cause problems and questions uvdos angular-material they must be reimported in the Karma questions uvdos angular-material TestBed as well. Also, it seems like we questions uvdos angular-material also need to import questions uvdos angular-material BrowserAnimationsModule to make questions uvdos angular-material everything work.
In my code below I have commented some questions uvdos angular-material modules that may not be useful for you, questions uvdos angular-material but the other 4 imports should questions uvdos angular-material definitely help !
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EventFormComponent ],
imports: [
# HttpClientTestingModule,
# RouterTestingModule,
ReactiveFormsModule,
MatFormFieldModule,
MatInputModule,
BrowserAnimationsModule,
# TranslateModule.forRoot(),
],
})
.compileComponents();
}));
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
1
Answers 20 : of mat-form-field must contain a MatFormFieldControl
You could try following this guide and questions uvdos angular-material implement/provide your own questions uvdos angular-material MatFormFieldControl
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
3
Answers 21 : of mat-form-field must contain a MatFormFieldControl
In my case I changed this:
<mat-form-field>
<input type="email" placeholder="email" [(ngModel)]="data.email">
</mat-form-field>
to this:
<mat-form-field>
<input matInput type="email" placeholder="email" [(ngModel)]="data.email">
</mat-form-field>
Adding the matInput directive to the questions uvdos angular-material input tag was what fixed this error for questions uvdos angular-material me.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
3
Answers 22 : of mat-form-field must contain a MatFormFieldControl
MatRadioModule won’t work inside questions uvdos angular-material MatFormField.
The docs say
This error occurs when you have not questions uvdos angular-material added a form field control to your
questions uvdos angular-material form field. If your form field contains questions uvdos angular-material a native or
element, make sure questions uvdos angular-material you’ve added the matInput directive to questions uvdos angular-material it and have
imported MatInputModule. questions uvdos angular-material Other components that can act as a form questions uvdos angular-material field
control include < questions uvdos angular-material mat-select>, < mat-chip-list>, questions uvdos angular-material and any custom form
field controls questions uvdos angular-material you’ve created.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
karim
2
Answers 23 : of mat-form-field must contain a MatFormFieldControl
In my case, one of my closing questions uvdos angular-material parenthesis for «onChanges()» were questions uvdos angular-material missed on the input element and thus the questions uvdos angular-material input element was apparently not being questions uvdos angular-material rendered at all:
<input mat-menu-item
matInput type="text"
[formControl]="myFormControl"
(ngModelChange)="onChanged()>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
2
Answers 24 : of mat-form-field must contain a MatFormFieldControl
You might have missed to import questions uvdos angular-material MatInputModule
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
yousuf
2
Answers 25 : of mat-form-field must contain a MatFormFieldControl
A partial solution is to wrap the questions uvdos angular-material material form field in a custom questions uvdos angular-material component and implement the questions uvdos angular-material ControlValueAccessor interface on it. questions uvdos angular-material Besides content projection the effect is questions uvdos angular-material pretty much the same.
See full example on Stackblitz.
I used FormControl (reactive forms) questions uvdos angular-material inside CustomInputComponent but questions uvdos angular-material FormGroup or FormArray should work too questions uvdos angular-material if you need a more complex form element.
app.component.html
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<mat-form-field appearance="outline" floatLabel="always" color="primary">
<mat-label>First name</mat-label>
<input matInput placeholder="First name" formControlName="firstName" required>
<mat-hint>Fill in first name.</mat-hint>
<mat-error *ngIf="firstNameControl.invalid && (firstNameControl.dirty || firstNameControl.touched)">
<span *ngIf="firstNameControl.hasError('required')">
You must fill in the first name.
</span>
</mat-error>
</mat-form-field>
<custom-input
formControlName="lastName"
[errorMessages]="errorMessagesForCustomInput"
[hint]="'Fill in last name.'"
[label]="'Last name'"
[isRequired]="true"
[placeholder]="'Last name'"
[validators]="validatorsForCustomInput">
</custom-input>
<button mat-flat-button
color="primary"
type="submit"
[disabled]="form.invalid || form.pending">
Submit
</button>
</form>
custom-input.component.html
<mat-form-field appearance="outline" floatLabel="always" color="primary">
<mat-label>{{ label }}</mat-label>
<input
matInput
[placeholder]="placeholder"
[required]="isRequired"
[formControl]="customControl"
(blur)="onTouched()"
(input)="onChange($event.target.value)">
<mat-hint>{{ hint }}</mat-hint>
<mat-error *ngIf="customControl.invalid && (customControl.dirty || customControl.touched)">
<ng-container *ngFor="let error of errorMessages">
<span *ngFor="let item of error | keyvalue">
<span *ngIf="customControl.hasError(item.key)">
{{ item.value }}
</span>
</span>
</ng-container>
</mat-error>
</mat-form-field>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
karim
4
Answers 26 : of mat-form-field must contain a MatFormFieldControl
You need to import the MatInputModule questions uvdos angular-material into your app.module.ts file
import { MatInputModule} from questions uvdos angular-material ‘@angular/material’;
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { CustomerPage } from './components/customer/customer';
import { CustomerDetailsPage } from "./components/customer-details/customer-details";
import { CustomerManagementPageRoutingModule } from './customer-management-routing.module';
import { MatAutocompleteModule } from '@angular/material/autocomplete'
import { ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule} from '@angular/material';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
CustomerManagementPageRoutingModule,
MatAutocompleteModule,
MatInputModule,
ReactiveFormsModule,
MatFormFieldModule
],
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
yousuf
3
Answers 27 : of mat-form-field must contain a MatFormFieldControl
You can set appearance=»fill» inside questions uvdos angular-material your mat-form-field tag, it works for me
<form class="example-form">
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Username Or Email</mat-label>
<input matInput placeholder="Username Or Email" type="text">
</mat-form-field>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Password</mat-label>
<input matInput placeholder="Password" type="password">
</mat-form-field>
</form>
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
joya
5
Answers 28 : of mat-form-field must contain a MatFormFieldControl
use providers in component.ts file
@Component({
selector: 'your-selector',
templateUrl: 'template.html',
styleUrls: ['style.css'],
providers: [
{ provide: MatFormFieldControl, useExisting: FormFieldCustomControlExample }
]
})
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
yousuf
6
Answers 29 : of mat-form-field must contain a MatFormFieldControl
Uncaught Error: mat-form-field must questions uvdos angular-material contain a MatFormFieldControl.
This error occurs when ‘MatInputModule’ questions uvdos angular-material is missed in imports,
import { MatInputModule } from '@angular/material/input'
import { MatFormFieldModule } from '@angular/material/form-field'
@NgModule({
imports: [
MatInputModule ,
MatFormFieldModule
]})
importing MatInputModule in the questions uvdos angular-material module.ts file would help getting rid of questions uvdos angular-material the problem.
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
rohim
6
Answers 30 : of mat-form-field must contain a MatFormFieldControl
Note
Some time Error occurs, when we use questions uvdos angular-material «mat-form-field» tag around submit questions uvdos angular-material button like:
<mat-form-field class="example-full-width">
<button mat-raised-button type="submit" color="primary"> Login</button>
</mat-form-field>
So kindly don’t use this tag around questions uvdos angular-material submit button
0
2023-02-09T18:16:22+00:00 2023-02-09T18:16:22+00:00Answer Link
karim