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 { }
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)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With