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
I Found a very simple solution without overriding the ErrorStateMatcher Class, simply you could import in the app.module.ts
Yes,
mat-error
does not show up by default. It only shows when the input istouched
.But, luckily you can override this behavior using
errorStateMatcher
input property, bound tomat-input
element.The pull request in which this feature was added.
Usage :
So you have to implement
ErrorStateMatcher
in your code this way.And in your component add a new object
matcher
forErrorStateMatcher
class, which will act as a value to[errorStateMatcher]="matcher"
I have also added the same code in your forked stackblitz
Suggestion :
You need not provide a
ngIf
condition format-error
specifying yourformControlName
. It will be automatically considered based on themat-form-field
in which it is present.