Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Can't bind to 'validationMessage' since it isn't a known property of 'mdc-text-field' #2271

@yalesnikov

Description

@yalesnikov

Hi, I tried to import TextField to my project and I had error.

I imported MdcTextFieldModule and added it to the import array into NgModule

my-module.ts

...
import { MdcFormFieldModule } from '@angular-mdc/web/form-field';
import { MdcTextFieldModule } from '@angular-mdc/web/textfield';
...

@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule,
    MdcFormFieldModule,
    MdcTextFieldModule,
    ...
  ], 

I used a validationMessage directive in html. I added this directive into mdc-text-field.

my-component.html

<form [formGroup]="demoForm" (ngSubmit)="submit(demoForm)">
 <mdc-form-field>
  <mdc-text-field 
    formControlName="username" 
    label="Username" 
    outlined
    [errorStateMatcher]="matcher"
    [validationMessage]="validationMessage"
    ></mdc-text-field>
 </mdc-form-field>
 <button mdc-button type="submit">Submit</button>
</form>

I used methods from example at official your site: https://trimox.github.io/angular-mdc-web/#/angular-mdc-web/text-field/examples

my-component.ts

import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, Validators, FormGroupDirective, NgForm } from '@angular/forms';
import { ErrorStateMatcher } from '@angular-mdc/web/form-field/typings/error-state-matcher';

export class APErrorStateMatcher implements ErrorStateMatcher {
  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    const isSubmitted = form && form.submitted;
    return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
  }
}

@Component({
  templateUrl: './my-component.html'
})
export class MyComponent implements OnInit {
  matcher = new APErrorStateMatcher();
  demoForm: FormGroup;

  ngOnInit(): void {
    this.demoForm = new FormGroup({
      username: new FormControl(
        { value: null, disabled: false },
        [Validators.required, Validators.minLength(3), Validators.maxLength(11)]
      )
    });
  }

  get validationMessage(): string {
    if (this.demoForm.controls['username'].hasError('minlength')) {
      return 'Username is not long enough';
    } else if (this.demoForm.controls['username'].hasError('maxlength')) {
      return 'Username is max length is 11';
    } else if (this.demoForm.controls['username'].hasError('required')) {
      return 'Field is required';
    }
    return undefined;
  }

  submit(f: NgForm | FormGroup) {
    if (f.invalid) { return; }
  }

But I had error:
Can't bind to 'validationMessage' since it isn't a known property of 'mdc-text-field'

I thought that your documentation is not full and I did not imported this directive or I incorrect imported textfiled. Can you help me please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions