Skip to content

bug(MatAutocomplete): Errors not displaying #28887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
patrickkforrit opened this issue Apr 16, 2024 · 6 comments
Closed
1 task done

bug(MatAutocomplete): Errors not displaying #28887

patrickkforrit opened this issue Apr 16, 2024 · 6 comments
Labels
area: material/autocomplete area: material/chips area: material/form-field P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@patrickkforrit
Copy link

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

14.3.0

Description

Having a Error to show alongside a mat-autocomplete and mat-chip-grid the error is never shown.

Reproduction

StackBlitz link: https://stackblitz.com/edit/uvatjk?file=src%2Fapp%2Fchips-autocomplete-example.ts,src%2Fapp%2Fchips-autocomplete-example.html
Steps to reproduce:

  1. Touch the control
  2. Enter a value
  3. Remove the value

Expected Behavior

Expected the mat-form-field to have an error state and the mat-error to show up

Actual Behavior

No feedback on any error and the tag is not shown in the dom

Environment

  • Angular: 15
  • CDK/Material: 15
  • Browser(s): Chrome
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows
@patrickkforrit patrickkforrit added the needs triage This issue needs to be triaged by the team label Apr 16, 2024
@patrickkforrit
Copy link
Author

Workaround use mat-hint and manually control visibility:

<mat-hint class="mat-mdc-form-field-error" *ngIf="conditionToShow">Error</mat-hint>

@patrickkforrit patrickkforrit changed the title bug(COMPONENT): TITLE bug(MatAutocomplete): Errors not displaying Apr 16, 2024
@zarend zarend added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: material/autocomplete area: material/form-field area: material/chips and removed needs triage This issue needs to be triaged by the team labels Apr 18, 2024
@tristinwhite1
Copy link

I have found that you need to put the form control on both the input and the mat-chip-grid for mat-error to work properly

@Cubelaster
Copy link

Cubelaster commented Apr 17, 2025

I am experiencing the same bug.
And I would call it a bug.

The mat-error requires a working formControl to ever be visible and explicitly linked to matInput.
If you don't mark your input of Autocomplete with any formControl, mat-error won't show.
However, this is also true if you're using NG_VALUE_ACCESSOR, which makes no sense at all.
If the input is linked to a formControl via NG_VALUE_ACCESSOR, mat-error should work.

This is especially true if you have a custom component because what? You should always make a copy of formControl even with VALUE_ACCESSOR?

mistrykaran91 added a commit to mistrykaran91/components that referenced this issue Apr 21, 2025
Currently, when multiple mat form fields are used together, the error is taken for the first control only, this take into consideration other controls too if they exist

Fixes angular#28887
mistrykaran91 added a commit to mistrykaran91/components that referenced this issue Apr 21, 2025
…eld controls

Currently, when multiple mat form fields are used together, the error is taken for the first control only, this take into consideration other controls too if they exist

Fixes angular#28887
@crisbeto
Copy link
Member

This is because the form control (chip grid in this case) isn't touched and invalid. Closing since it's working as expected.

@Cubelaster
Copy link

I solved this and this is "not a bug" but rather very poor documentation.
All instances of form-field + mat-error need to be inside the same level to work properly because the built in selectors do not work properly.
So if you have matAutocomplete in the form-field and mat-error at the same place, everything works fine.
If you, however, have mat-error in a custom component outside of original form, it will not work properly.
Even if you implement ValueAccessor and MatFormFieldControl, mat-error won't show properly at all.
That is until you provide NgModel as well, then it miracuosly works.

@volodymyrsamoilenkoscl
Copy link

@Cubelaster do you mind to drop an example of working example with mar-error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/autocomplete area: material/chips area: material/form-field P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
6 participants