Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mat-error not get displayed inside mat-form field in angular material 6 withcustom global validators

Tags:

i am using angular material 6 ,i have a vaidation inside mat-form-field mat-error is not displayed , when move after mat-form-field to the mat-error which is displaying properly.

Not Working code:

 <mat-form-field>
<input matInput type="time" formControlName="ToTime"/> <mat-error *ngIf="DaterForm.get('ToTime').hasError('InValidToTime')">FromTime Should be less than ToTime if choose a same date</mat-error>
     </mat-form-field>

Working Fine:

 <input matInput type="time" formControlName="ToTime"/> </mat-form-field>
 <mat-error *ngIf="DaterForm.get('ToTime').hasError('InValidToTime')">FromTime Should be less than ToTime if choose a same date</mat-error>

Some one explain why which is not working inside that control.

Live Demo: stackblitz

like image 395
Mohamed Sahir Avatar asked Jul 21 '18 13:07

Mohamed Sahir


People also ask

How do I display mat error?

Error messages can be shown under the form field underline by adding mat-error elements inside the form field. Errors are hidden initially and will be displayed on invalid form fields after the user has interacted with the element or the parent form has been submitted.

How do I fix mat form field must contain a Matformfieldcontrol?

To fix the error, add MatInputModule and MatFormFieldModule inside a necessary module or app.

Why do we use Matinput?

Matinput is an Angular directive that primarily allows input and text area elements to work with a form field. With this, you can display placeholders perfectly, add custom error messages, a clear button, specify the maximum length of the text or add prefixes and suffixes for a seamless user experience.

What is Mat input in angular?

matInput is a directive that allows native <input> and <textarea> elements to work with <mat-form-field> .


1 Answers

Yes, mat-error does not show up by default. It only shows when the input is touched.

But, luckily you can override this behavior using errorStateMatcher input property, bound to mat-input element.

The pull request in which this feature was added.

Usage :

<mat-form-field>
    <input matInput [errorStateMatcher]="matcher" [matDatepicker]="picker" placeholder="Choose a Start date" 
    formControlName="FromDate"
      [min]="minFromDate" 
           [max]="maxToDate" >
    <mat-datepicker-toggle matSuffix [for]="picker" ></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
    <mat-error >Please provide a valid Fromdate</mat-error> 
  </mat-form-field> 

So you have to implement ErrorStateMatcher in your code this way.

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

And in your component add a new object matcher for ErrorStateMatcher class, which will act as a value to [errorStateMatcher]="matcher"

matcher = new MyErrorStateMatcher();

I have also added the same code in your forked stackblitz

Suggestion :

You need not provide a ngIf condition for mat-error specifying your formControlName. It will be automatically considered based on the mat-form-field in which it is present.

like image 191
Amit Chigadani Avatar answered Sep 21 '22 20:09

Amit Chigadani