1

I am trying to use md-error tag to display error messages on an mdAutocomplete. I am using Angular with Material 2 components. The md-error message is displayed for built-in validators like required. However, I also want to display another error message with md-error when No Matching Records are found (basically when user types something which is not there in the dropdown). So, I tried having another md-error with an *ngIf but this error message is never shown. I googled about it and looks like the solution is to have a custom validation method or Directive. Material 2 provides a way to have custom validation method but I am not able to use it with my template based form. Could anyone provide me an example of using custom validation on a template based form using md-error?

Sample Code:

This didn't work:

<md-input-container>
  <input mdInput
         placeholder="Currency Type"
         [(ngModel)]="policyObj.cost.premium.currencyType.name"
         [mdAutocomplete]="premiumCurrencyTypeAuto"
         [disabled]="disable"
         (keydown)="PanelOptions($event, policyObj.cost.premium.currencyType, filteredPremiumCurrencyType, premiumCurrencyTypeAuto)"
         (ngModelChange)="filteredPremiumCurrencyType = filterCurrency(policyObj.cost.premium.currencyType.name)"
         name="currency_type1" required>
     <md-error>This field is required</md-error>
     <md-error *ngIf="filteredPremiumCurrencyType?.length === 0"> No Matching
        Records Found!</md-error>
</md-input-container>

<md-autocomplete #premiumCurrencyTypeAuto="mdAutocomplete" [displayWith]="displayFn">
  <md-option *ngFor="let ct of filteredPremiumCurrencyType" [value]="ct">
    {{ ct.name }}
  </md-option>
</md-autocomplete>

I tried looking at Material 2 - Custom Error Matcher but was not sure on how to use it in my case with template based forms. Any help would be appreciated. Thanks!

0

1 Answer 1

2

errorStateMatcher should work just the same with template and reactive forms. Something like this should work for your use case:

<md-input-container>
  <input mdInput [errorStateMatcher]="myErrorStateMatcher.bind(this)" ... >
  <md-error *ngIf="policyObj.cost.premium.currencyType.name === ''">This field is required</md-error>
  <md-error *ngIf="filteredPremiumCurrencyType?.length === 0" No Matching Records Found!</md-error>
</md-input-container>


function myErrorStateMatcher(control, form): boolean {
  if (this.filteredPremiumCurrencyType && !this.filteredPremiumCurrencyType.length) {
    return true;
  }

  // Error when invalid control is touched, or submitted
  const isSubmitted = form && form.submitted;
  return !!(control.invalid && (control.touched || isSubmitted)));
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for posting an answer. I tried the way you have mentioned but then it displays both the md-error messages. How can I have a *ngIf on the second md-error as shown in above code snippet.
Add an *ngIf to the required: <md-error *ngIf="currency_type1.errors?.required"> This field is required </md-error> seemed to work when I tried it, if it works, Will can probably update that to answer :)
Updated! Impossible to know if I got the details right, but @mrsan22, just use *ngIf on the errors however you wish
Thanks guys...having *ngIf on the required worked.
@WillHowell I have tried the way you told but getting some issue . can you please help me out.If needed i will start a separate thread for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.